use crate::common::app_context::AppContext;
use crate::common::authentication::get_api_token;
use crate::common::session::SessionContext;
use crate::http_client::MantaClient;
use anyhow::{Error, bail};
use clap::ArgMatches;
use crate::dispatch::{
add, apply, backup, config, console, delete, gen_autocomplete, gen_man, get,
log, migrate, power, restore, run, upgrade,
};
fn verb_skips_session(cli_root: &ArgMatches) -> bool {
match cli_root.subcommand() {
Some(("gen-autocomplete", _)) | Some(("gen-man", _)) => true,
Some(("upgrade", _)) => true,
Some(("config", config_m)) => match config_m.subcommand() {
Some(("set", set_m)) => matches!(
set_m.subcommand(),
Some(("site", _)) | Some(("log", _)) | Some(("read-only", _))
),
Some(("unset", unset_m)) => matches!(
unset_m.subcommand(),
Some(("hsm", _)) | Some(("auth", _)) | Some(("read-only", _))
),
_ => false,
},
_ => false,
}
}
pub async fn process_cli(
cli_root: &ArgMatches,
mut ctx: AppContext<'_>,
) -> Result<(), Error> {
crate::common::read_only::read_only_gate(cli_root, ctx.read_only)?;
let needs_session = !verb_skips_session(cli_root) && ctx.site_name.is_some();
let token_opt: Option<String> = if needs_session {
Some(get_api_token(&ctx).await?)
} else {
None
};
if let Some(token) = token_opt {
let client = MantaClient::from_app_ctx(&ctx, Some(&token))?;
ctx.session = Some(SessionContext::build(&client, &token).await?);
ctx.token = Some(token);
}
match cli_root.subcommand() {
Some(("config", m)) => config::handle_config(m, &ctx).await?,
Some(("power", m)) => power::handle_power(m, &ctx).await?,
Some(("add", m)) => add::handle_add(m, &ctx).await?,
Some(("get", m)) => get::handle_get(m, &ctx).await?,
Some(("apply", m)) => apply::handle_apply(m, &ctx).await?,
Some(("log", m)) => log::handle_log(m, &ctx).await?,
Some(("console", m)) => console::handle_console(m, &ctx).await?,
Some(("migrate", m)) => migrate::handle_migrate(m, &ctx).await?,
Some(("backup", m)) => backup::handle_backup(m, &ctx).await?,
Some(("restore", m)) => restore::handle_restore(m, &ctx).await?,
Some(("run", m)) => run::handle_run(m, &ctx).await?,
Some(("delete", m)) => delete::handle_delete(m, &ctx).await?,
Some(("gen-autocomplete", m)) => {
gen_autocomplete::handle_gen_autocomplete(m, &ctx).await?;
}
Some(("gen-man", m)) => gen_man::handle_gen_man(m, &ctx).await?,
Some(("upgrade", m)) => upgrade::handle_upgrade(m, &ctx).await?,
Some((other, _)) => bail!("Unknown command: {other}"),
None => bail!("No command provided"),
}
Ok(())
}