spacetimedb_cli/subcommands/
energy.rs1use crate::common_args;
3use clap::ArgMatches;
4
5use crate::config::Config;
6use crate::util::{self, get_login_token_or_log_in, UNSTABLE_WARNING};
7
8pub fn cli() -> clap::Command {
9 clap::Command::new("energy")
10 .about(format!(
11 "Invokes commands related to database budgets. {UNSTABLE_WARNING}"
12 ))
13 .args_conflicts_with_subcommands(true)
14 .subcommand_required(true)
15 .subcommands(get_energy_subcommands())
16}
17
18fn get_energy_subcommands() -> Vec<clap::Command> {
19 vec![clap::Command::new("balance")
20 .about("Show current energy balance for an identity")
21 .arg(
22 common_args::identity()
23 .help("The identity to check the balance for")
24 .long_help(
25 "The identity to check the balance for. If no identity is provided, the default one will be used.",
26 ),
27 )
28 .arg(
29 common_args::server()
30 .help("The nickname, host name or URL of the server from which to request balance information"),
31 )
32 .arg(common_args::yes())]
33}
34
35async fn exec_subcommand(config: Config, cmd: &str, args: &ArgMatches) -> Result<(), anyhow::Error> {
36 match cmd {
37 "balance" => exec_status(config, args).await,
38 unknown => Err(anyhow::anyhow!("Invalid subcommand: {}", unknown)),
39 }
40}
41
42pub async fn exec(config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
43 let (cmd, subcommand_args) = args.subcommand().expect("Subcommand required");
44 eprintln!("{UNSTABLE_WARNING}\n");
45 exec_subcommand(config, cmd, subcommand_args).await
46}
47
48async fn exec_status(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
49 let identity = args.get_one::<String>("identity");
51 let server = args.get_one::<String>("server").map(|s| s.as_ref());
52 let force = args.get_flag("force");
53 let identity = if let Some(identity) = identity {
55 identity.clone()
56 } else {
57 let token = get_login_token_or_log_in(&mut config, server, !force).await?;
58 util::decode_identity(&token)?
59 };
60
61 let status = reqwest::Client::new()
62 .get(format!("{}/v1/energy/{}", config.get_host_url(server)?, identity))
63 .send()
64 .await?
65 .error_for_status()?
66 .text()
67 .await?;
68
69 println!("{status}");
70
71 Ok(())
72}