gateway_cli/
main.rs

1#![deny(clippy::pedantic, clippy::nursery)]
2
3mod config_commands;
4mod ecash_commands;
5mod general_commands;
6mod lightning_commands;
7mod onchain_commands;
8
9use clap::{CommandFactory, Parser, Subcommand};
10use config_commands::ConfigCommands;
11use ecash_commands::EcashCommands;
12use fedimint_core::util::SafeUrl;
13use fedimint_gateway_client::GatewayRpcClient;
14use fedimint_gateway_common::V1_API_ENDPOINT;
15use fedimint_logging::TracingSetup;
16use general_commands::GeneralCommands;
17use lightning_commands::LightningCommands;
18use onchain_commands::OnchainCommands;
19use serde::Serialize;
20
21#[derive(Parser)]
22#[command(version)]
23struct Cli {
24    /// The address of the gateway webserver
25    #[clap(short, long, default_value = "http://127.0.0.1:8175")]
26    address: SafeUrl,
27    #[command(subcommand)]
28    command: Commands,
29    /// WARNING: Passing in a password from the command line may be less secure!
30    #[clap(long)]
31    rpcpassword: Option<String>,
32}
33
34#[derive(Subcommand)]
35enum Commands {
36    #[command(flatten)]
37    General(GeneralCommands),
38    #[command(subcommand)]
39    Lightning(LightningCommands),
40    #[command(subcommand)]
41    Ecash(EcashCommands),
42    #[command(subcommand)]
43    Onchain(OnchainCommands),
44    #[command(subcommand)]
45    Cfg(ConfigCommands),
46    Completion {
47        shell: clap_complete::Shell,
48    },
49}
50
51#[tokio::main]
52async fn main() -> anyhow::Result<()> {
53    TracingSetup::default().init()?;
54
55    let cli = Cli::parse();
56    let versioned_api = cli.address.join(V1_API_ENDPOINT)?;
57    let create_client = || GatewayRpcClient::new(versioned_api.clone(), cli.rpcpassword.clone());
58
59    match cli.command {
60        Commands::General(general_command) => general_command.handle(create_client).await?,
61        Commands::Lightning(lightning_command) => lightning_command.handle(create_client).await?,
62        Commands::Ecash(ecash_command) => ecash_command.handle(create_client).await?,
63        Commands::Onchain(onchain_command) => onchain_command.handle(create_client).await?,
64        Commands::Cfg(config_commands) => config_commands.handle(create_client).await?,
65        Commands::Completion { shell } => {
66            clap_complete::generate(
67                shell,
68                &mut Cli::command(),
69                "gateway-cli",
70                &mut std::io::stdout(),
71            );
72        }
73    }
74
75    Ok(())
76}
77
78fn print_response<T: Serialize>(val: T) {
79    println!(
80        "{}",
81        serde_json::to_string_pretty(&val).expect("Cannot serialize")
82    );
83}