1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Node commands
use clap::Subcommand;

use crate::rpc::client::node;

/// Commands to manage the iroh RPC.
#[derive(Subcommand, Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum NodeCommands {
    /// Get statistics and metrics from the running node.
    Stats,
    /// Get status of the running node.
    Status,
    /// Shutdown the running node.
    Shutdown {
        /// Shutdown mode.
        ///
        /// Hard shutdown will immediately terminate the process, soft shutdown will wait
        /// for all connections to close.
        #[clap(long, default_value_t = false)]
        force: bool,
    },
}

impl NodeCommands {
    /// Run the RPC command given the iroh client and the console environment.
    pub async fn run(self, node: &node::Client) -> anyhow::Result<()> {
        match self {
            Self::Stats => {
                let stats = node.stats().await?;
                for (name, details) in stats.iter() {
                    println!(
                        "{:23} : {:>6}    ({})",
                        name, details.value, details.description
                    );
                }
                Ok(())
            }
            Self::Shutdown { force } => {
                node.shutdown(force).await?;
                Ok(())
            }
            Self::Status => {
                let response = node.status().await?;
                println!("Listening addresses: {:#?}", response.listen_addrs);
                println!("Node ID: {}", response.addr.node_id);
                println!("Version: {}", response.version);
                if let Some(addr) = response.rpc_addr {
                    println!("RPC Addr: {}", addr);
                }
                Ok(())
            }
        }
    }
}