soroban_cli/commands/network/
health.rs

1use crate::commands::global;
2use crate::config::network;
3use crate::{config, print};
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}
14
15#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum, Default)]
16pub enum OutputFormat {
17    /// Text output of network health status
18    #[default]
19    Text,
20    /// JSON result of the RPC request
21    Json,
22    /// Formatted (multiline) JSON output of the RPC request
23    JsonFormatted,
24}
25
26#[derive(Debug, clap::Parser, Clone)]
27#[group(skip)]
28pub struct Cmd {
29    #[command(flatten)]
30    pub config: config::ArgsLocatorAndNetwork,
31    /// Format of the output
32    #[arg(long, default_value = "text")]
33    pub output: OutputFormat,
34}
35
36impl Cmd {
37    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
38        let print = print::Print::new(global_args.quiet);
39        let result = self.config.get_network()?.rpc_client()?.get_health().await;
40
41        match result {
42            Ok(resp) => match self.output {
43                OutputFormat::Text => {
44                    if resp.status.eq_ignore_ascii_case("healthy") {
45                        print.checkln("Healthy");
46                    } else {
47                        print.warnln(format!("Status: {}", resp.status));
48                    }
49                    print.infoln(format!("Latest ledger: {}", resp.latest_ledger));
50                }
51                OutputFormat::Json => println!("{}", serde_json::to_string(&resp)?),
52                OutputFormat::JsonFormatted => println!("{}", serde_json::to_string_pretty(&resp)?),
53            },
54            Err(err) => {
55                print.errorln("Unhealthy");
56                print.errorln(format!("failed to fetch network health: {err}"));
57            }
58        }
59
60        Ok(())
61    }
62}