iroh_node_util/cli/
node.rs

1//! Node commands
2use clap::Subcommand;
3
4use crate::rpc::client::node;
5
6/// Commands to manage the iroh RPC.
7#[derive(Subcommand, Debug, Clone)]
8#[allow(clippy::large_enum_variant)]
9pub enum NodeCommands {
10    /// Get statistics and metrics from the running node.
11    Stats,
12    /// Get status of the running node.
13    Status,
14    /// Shutdown the running node.
15    Shutdown {
16        /// Shutdown mode.
17        ///
18        /// Hard shutdown will immediately terminate the process, soft shutdown will wait
19        /// for all connections to close.
20        #[clap(long, default_value_t = false)]
21        force: bool,
22    },
23}
24
25impl NodeCommands {
26    /// Run the RPC command given the iroh client and the console environment.
27    pub async fn run(self, node: &node::Client) -> anyhow::Result<()> {
28        match self {
29            Self::Stats => {
30                let stats = node.stats().await?;
31                for (name, details) in stats.iter() {
32                    println!(
33                        "{:23} : {:>6}    ({})",
34                        name, details.value, details.description
35                    );
36                }
37                Ok(())
38            }
39            Self::Shutdown { force } => {
40                node.shutdown(force).await?;
41                Ok(())
42            }
43            Self::Status => {
44                let response = node.status().await?;
45                println!("Listening addresses: {:#?}", response.listen_addrs);
46                println!("Node ID: {}", response.addr.node_id);
47                println!("Version: {}", response.version);
48                if let Some(addr) = response.rpc_addr {
49                    println!("RPC Addr: {}", addr);
50                }
51                Ok(())
52            }
53        }
54    }
55}