kratactl/cli/host/
status.rs

1use anyhow::Result;
2use clap::{Parser, ValueEnum};
3use krata::v1::control::{control_service_client::ControlServiceClient, GetHostStatusRequest};
4
5use crate::format::{kv2line, proto2dynamic, proto2kv};
6use tonic::{transport::Channel, Request};
7
8#[derive(ValueEnum, Clone, Debug, PartialEq, Eq)]
9enum HostStatusFormat {
10    Simple,
11    Json,
12    JsonPretty,
13    Yaml,
14    KeyValue,
15}
16
17#[derive(Parser)]
18#[command(about = "Get information about the host")]
19pub struct HostStatusCommand {
20    #[arg(short, long, default_value = "simple", help = "Output format")]
21    format: HostStatusFormat,
22}
23
24impl HostStatusCommand {
25    pub async fn run(self, mut client: ControlServiceClient<Channel>) -> Result<()> {
26        let response = client
27            .get_host_status(Request::new(GetHostStatusRequest {}))
28            .await?
29            .into_inner();
30        match self.format {
31            HostStatusFormat::Simple => {
32                println!("Host UUID: {}", response.host_uuid);
33                println!("Host Domain: {}", response.host_domid);
34                println!("Krata Version: {}", response.krata_version);
35                println!("Host IPv4: {}", response.host_ipv4);
36                println!("Host IPv6: {}", response.host_ipv6);
37                println!("Host Ethernet Address: {}", response.host_mac);
38            }
39
40            HostStatusFormat::Json | HostStatusFormat::JsonPretty | HostStatusFormat::Yaml => {
41                let message = proto2dynamic(response)?;
42                let value = serde_json::to_value(message)?;
43                let encoded = if self.format == HostStatusFormat::JsonPretty {
44                    serde_json::to_string_pretty(&value)?
45                } else if self.format == HostStatusFormat::Yaml {
46                    serde_yaml::to_string(&value)?
47                } else {
48                    serde_json::to_string(&value)?
49                };
50                println!("{}", encoded.trim());
51            }
52
53            HostStatusFormat::KeyValue => {
54                let kvs = proto2kv(response)?;
55                println!("{}", kv2line(kvs),);
56            }
57        }
58        Ok(())
59    }
60}