From a4f8dba59b55e5f09a4c723e34ad3557040c32c7 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Tue, 27 Aug 2024 09:01:04 +0200 Subject: [PATCH] Small refactoring: share the HTTP Client initialization for CLI cmds --- rust/agama-cli/src/main.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/rust/agama-cli/src/main.rs b/rust/agama-cli/src/main.rs index 48e83609a9..e2edf3047a 100644 --- a/rust/agama-cli/src/main.rs +++ b/rust/agama-cli/src/main.rs @@ -132,13 +132,25 @@ async fn build_manager<'a>() -> anyhow::Result> { } async fn run_command(cli: Cli) -> Result<(), ServiceError> { - // we need to distinguish commands on those which assume that JWT is already available - // and those which not (or don't need it) + // we need to distinguish commands on those which assume that authentication JWT is already + // available and those which not (or don't need it) + let mut client = if matches!(cli.command, Commands::Auth(_)) { + BaseHTTPClient::default() + } + else { + // this deals with authentication need inside + BaseHTTPClient::new()? + }; + + client.base_url = cli + .opts + .api + .strip_suffix("/") + .unwrap_or(client.base_url.as_str()) + .to_string(); + match cli.command { Commands::Config(subcommand) => { - // this deals with authentication need inside - let client = BaseHTTPClient::new()?; - run_config_cmd(client, subcommand).await? } Commands::Probe => { @@ -157,15 +169,6 @@ async fn run_command(cli: Cli) -> Result<(), ServiceError> { Commands::Logs(subcommand) => run_logs_cmd(subcommand).await?, Commands::Download { url } => crate::profile::download(&url, std::io::stdout())?, Commands::Auth(subcommand) => { - let mut client = BaseHTTPClient::default(); - - client.base_url = cli - .opts - .api - .strip_suffix("/") - .unwrap_or(client.base_url.as_str()) - .to_string(); - run_auth_cmd(client, subcommand).await?; } };