From 933d217b63d89666001b557c3777607c3fd8b315 Mon Sep 17 00:00:00 2001 From: ByteDream Date: Wed, 7 Dec 2022 20:08:44 +0100 Subject: [PATCH] Add pre_check checking --- crunchy-cli-core/src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crunchy-cli-core/src/lib.rs b/crunchy-cli-core/src/lib.rs index c895d51..2e0cf4d 100644 --- a/crunchy-cli-core/src/lib.rs +++ b/crunchy-cli-core/src/lib.rs @@ -152,18 +152,28 @@ pub async fn cli_entrypoint() { .unwrap(); debug!("Created ctrl-c handler"); - let result = match cli.command { - Command::Archive(archive) => archive.execute(ctx).await, - Command::Download(download) => download.execute(ctx).await, + match cli.command { + Command::Archive(archive) => execute_executor(archive,ctx).await, + Command::Download(download) => execute_executor(download, ctx).await, Command::Login(login) => { if login.remove { - Ok(()) + return; } else { - login.execute(ctx).await + execute_executor(login, ctx).await } } }; - if let Err(err) = result { +} + +/// Cannot be done in the main function. I wanted to return `dyn` [`Execute`] from the match but had to +/// box it which then conflicts with [`Execute::execute`] which consumes `self` +async fn execute_executor(executor: impl Execute, ctx: Context) { + if let Err(err) = executor.pre_check() { + error!("Misconfigurations detected: {}", err); + std::process::exit(1) + } + + if let Err(err) = executor.execute(ctx).await { error!("a unexpected error occurred: {}", err); std::process::exit(1) }