soroban_cli/commands/network/
info.rs

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