Skip to content

Commit

Permalink
Small refactoring: share the HTTP Client initialization for CLI cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Aug 27, 2024
1 parent 3fe0c3a commit a4f8dba
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions rust/agama-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,25 @@ async fn build_manager<'a>() -> anyhow::Result<ManagerClient<'a>> {
}

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 => {
Expand All @@ -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?;
}
};
Expand Down

0 comments on commit a4f8dba

Please sign in to comment.