soroban_cli/commands/network/
info.rs

1use crate::commands::global;
2use crate::config::network;
3use crate::{config, print, rpc};
4use clap::command;
5
6#[derive(thiserror::Error, Debug)]
7pub enum Error {
8    #[error(transparent)]
9    Config(#[from] config::Error),
10    #[error(transparent)]
11    Network(#[from] network::Error),
12    #[error(transparent)]
13    Serde(#[from] serde_json::Error),
14    #[error(transparent)]
15    Rpc(#[from] rpc::Error),
16}
17
18#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum, Default)]
19pub enum OutputFormat {
20    /// Text output of network info
21    #[default]
22    Text,
23    /// JSON result of the RPC request
24    Json,
25    /// Formatted (multiline) JSON output of the RPC request
26    JsonFormatted,
27}
28
29#[derive(Debug, clap::Parser, Clone)]
30#[group(skip)]
31pub struct Cmd {
32    #[command(flatten)]
33    pub config: config::ArgsLocatorAndNetwork,
34    /// Format of the output
35    #[arg(long, default_value = "text")]
36    pub output: OutputFormat,
37}
38
39#[derive(serde::Deserialize, serde::Serialize)]
40struct Info {
41    pub version: String,
42    pub commit_hash: String,
43    pub build_timestamp: String,
44    pub captive_core_version: String,
45    pub protocol_version: u32,
46    pub passphrase: String,
47    pub friendbot_url: Option<String>,
48}
49
50impl Info {
51    fn print_text(&self, print: &print::Print) {
52        print.infoln(format!("Version: {}", self.version));
53        print.infoln(format!("Commit Hash: {}", self.commit_hash));
54        print.infoln(format!("Build Timestamp: {}", self.build_timestamp));
55        print.infoln(format!(
56            "Captive Core Version: {}",
57            self.captive_core_version
58        ));
59        print.infoln(format!("Protocol Version: {}", self.protocol_version));
60        print.infoln(format!("Passphrase: {}", self.passphrase));
61        if let Some(friendbot_url) = &self.friendbot_url {
62            print.infoln(format!("Friendbot Url: {friendbot_url}"));
63        }
64    }
65
66    fn print_json(&self) -> Result<(), serde_json::Error> {
67        let json = serde_json::to_string(&self)?;
68        println!("{json}");
69        Ok(())
70    }
71
72    fn print_json_formatted(&self) -> Result<(), serde_json::Error> {
73        let json = serde_json::to_string_pretty(&self)?;
74        println!("{json}");
75        Ok(())
76    }
77}
78
79impl Cmd {
80    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
81        let print = print::Print::new(global_args.quiet);
82        let rpc_client = self.config.get_network()?.rpc_client()?;
83        let network_result = rpc_client.get_network().await?;
84        let version_result = rpc_client.get_version_info().await?;
85        let info = Info {
86            version: version_result.version,
87            commit_hash: version_result.commmit_hash,
88            build_timestamp: version_result.build_timestamp,
89            captive_core_version: version_result.captive_core_version,
90            protocol_version: network_result.protocol_version,
91            friendbot_url: network_result.friendbot_url,
92            passphrase: network_result.passphrase,
93        };
94
95        match self.output {
96            OutputFormat::Text => info.print_text(&print),
97            OutputFormat::Json => info.print_json()?,
98            OutputFormat::JsonFormatted => info.print_json_formatted()?,
99        }
100
101        Ok(())
102    }
103}