From 39395c797af6500e8e4b3f8b39e6a385783b204a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20P=2E=20Bezerra?= Date: Fri, 15 Mar 2024 16:33:55 -0300 Subject: [PATCH] move code to function `run_cmd` --- src/commands/mod.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index be96232..9305e46 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -59,10 +59,29 @@ pub fn run( ) -> crate::Result<()> { let log_receiver = setup_channel(); - let pair = Arc::new((Mutex::new(false), Condvar::new())); + let synchronization_pair = Arc::new((Mutex::new(false), Condvar::new())); + spawn_logger_thread(log_receiver, synchronization_pair.clone()); + run_cmd(args, question_policy, file_visibility_policy)?; - spawn_logger_thread(log_receiver, pair.clone()); + // Drop our sender so when all threads are done, no clones are left. + // This is needed, otherwise the logging thread will never exit since we would be keeping a + // sender alive here. + todo!(); + // Prevent the main thread from exiting until the background thread handling the + // logging has set `flushed` to true. + let (lock, cvar) = &*synchronization_pair; + let guard = lock.lock().unwrap(); + let _flushed = cvar.wait(guard).unwrap(); + + Ok(()) +} + +fn run_cmd( + args: CliArgs, + question_policy: QuestionPolicy, + file_visibility_policy: FileVisibilityPolicy, +) -> crate::Result<()> { match args.cmd { Subcommand::Compress { files, @@ -143,7 +162,7 @@ pub fn run( } } - compress_result?; + compress_result.map(|_| ()) } Subcommand::Decompress { files, output_dir } => { let mut output_paths = vec![]; @@ -196,7 +215,7 @@ pub fn run( question_policy, args.quiet, ) - })?; + }) } Subcommand::List { archives: files, tree } => { let mut formats = vec![]; @@ -230,21 +249,10 @@ pub fn run( let formats = extension::flatten_compression_formats(&formats); list_archive_contents(archive_path, formats, list_options, question_policy)?; } + + Ok(()) } } - - // Drop our sender so when all threads are done, no clones are left. - // This is needed, otherwise the logging thread will never exit since we would be keeping a - // sender alive here. - todo!(); - - // Prevent the main thread from exiting until the background thread handling the - // logging has set `flushed` to true. - let (lock, cvar) = &*pair; - let guard = lock.lock().unwrap(); - let _flushed = cvar.wait(guard).unwrap(); - - Ok(()) } fn spawn_logger_thread(log_receiver: LogReceiver, synchronization_pair: Arc<(Mutex, Condvar)>) {