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() {
53    if let Err(err) = TracingSetup::default().init() {
54        eprintln!("Failed to initialize logging: {err}");
55        std::process::exit(1);
56    }
57
58    if let Err(err) = run().await {
59        eprintln!("Error: {err}");
60        let mut source = err.source();
61        eprintln!("Caused by");
62        while let Some(err) = source {
63            eprintln!("    {err}");
64            source = err.source();
65        }
66        std::process::exit(1);
67    }
68}
69
70async fn run() -> anyhow::Result<()> {
71    let cli = Cli::parse();
72    let versioned_api = cli.address.join(V1_API_ENDPOINT)?;
73    let create_client = || GatewayRpcClient::new(versioned_api.clone(), cli.rpcpassword.clone());
74
75    match cli.command {
76        Commands::General(general_command) => general_command.handle(create_client).await?,
77        Commands::Lightning(lightning_command) => lightning_command.handle(create_client).await?,
78        Commands::Ecash(ecash_command) => ecash_command.handle(create_client).await?,
79        Commands::Onchain(onchain_command) => onchain_command.handle(create_client).await?,
80        Commands::Cfg(config_commands) => config_commands.handle(create_client).await?,
81        Commands::Completion { shell } => {
82            clap_complete::generate(
83                shell,
84                &mut Cli::command(),
85                "gateway-cli",
86                &mut std::io::stdout(),
87            );
88        }
89    }
90
91    Ok(())
92}
93
94fn print_response<T: Serialize>(val: T) {
95    println!(
96        "{}",
97        serde_json::to_string_pretty(&val).expect("Cannot serialize")
98    );
99}