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