use crate::common_args;
use clap::ArgMatches;
use crate::config::Config;
use crate::util::{self, get_login_token_or_log_in, UNSTABLE_WARNING};
pub fn cli() -> clap::Command {
clap::Command::new("energy")
.about(format!(
"Invokes commands related to database budgets. {}",
UNSTABLE_WARNING
))
.args_conflicts_with_subcommands(true)
.subcommand_required(true)
.subcommands(get_energy_subcommands())
}
fn get_energy_subcommands() -> Vec<clap::Command> {
vec![clap::Command::new("balance")
.about("Show current energy balance for an identity")
.arg(
common_args::identity()
.help("The identity to check the balance for")
.long_help(
"The identity to check the balance for. If no identity is provided, the default one will be used.",
),
)
.arg(
common_args::server()
.help("The nickname, host name or URL of the server from which to request balance information"),
)
.arg(common_args::yes())]
}
async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result<(), anyhow::Error> {
match cmd {
"balance" => exec_status(config, args).await,
unknown => Err(anyhow::anyhow!("Invalid subcommand: {}", unknown)),
}
}
pub async fn exec(config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
let (cmd, subcommand_args) = args.subcommand().expect("Subcommand required");
eprintln!("{}\n", UNSTABLE_WARNING);
exec_subcommand(config, cmd, subcommand_args).await
}
async fn exec_status(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
let identity = args.get_one::<String>("identity");
let server = args.get_one::<String>("server").map(|s| s.as_ref());
let force = args.get_flag("force");
let identity = if let Some(identity) = identity {
identity.clone()
} else {
let token = get_login_token_or_log_in(&mut config, server, !force).await?;
util::decode_identity(&token)?
};
let status = reqwest::Client::new()
.get(format!("{}/v1/energy/{}", config.get_host_url(server)?, identity))
.send()
.await?
.error_for_status()?
.text()
.await?;
println!("{}", status);
Ok(())
}