iggy_cli/commands/binary_client/
get_clients.rs1use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::Table;
23use iggy_common::Client;
24use tracing::{Level, event};
25
26pub enum GetClientsOutput {
27 Table,
28 List,
29}
30
31pub struct GetClientsCmd {
32 output: GetClientsOutput,
33}
34
35impl GetClientsCmd {
36 pub fn new(output: GetClientsOutput) -> Self {
37 GetClientsCmd { output }
38 }
39}
40
41impl Default for GetClientsCmd {
42 fn default() -> Self {
43 GetClientsCmd {
44 output: GetClientsOutput::Table,
45 }
46 }
47}
48
49#[async_trait]
50impl CliCommand for GetClientsCmd {
51 fn explain(&self) -> String {
52 let mode = match self.output {
53 GetClientsOutput::Table => "table",
54 GetClientsOutput::List => "list",
55 };
56 format!("list clients in {mode} mode")
57 }
58
59 async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
60 let clients = client
61 .get_clients()
62 .await
63 .with_context(|| String::from("Problem getting list of clients"))?;
64
65 if clients.is_empty() {
66 event!(target: PRINT_TARGET, Level::INFO, "No clients found!");
67 return Ok(());
68 }
69
70 match self.output {
71 GetClientsOutput::Table => {
72 let mut table = Table::new();
73
74 table.set_header(vec![
75 "Client ID",
76 "User ID",
77 "Address",
78 "Transport",
79 "Consumer Groups",
80 ]);
81
82 clients.iter().for_each(|client_info| {
83 table.add_row(vec![
84 format!("{}", client_info.client_id),
85 match client_info.user_id {
86 Some(user_id) => format!("{user_id}"),
87 None => String::from(""),
88 },
89 format!("{}", client_info.address),
90 format!("{}", client_info.transport),
91 format!("{}", client_info.consumer_groups_count),
92 ]);
93 });
94
95 event!(target: PRINT_TARGET, Level::INFO, "{table}");
96 }
97 GetClientsOutput::List => {
98 clients.iter().for_each(|client_info| {
99 event!(target: PRINT_TARGET, Level::INFO,
100 "{}|{}|{}|{}|{}",
101 client_info.client_id,
102 match client_info.user_id {
103 Some(user_id) => format!("{user_id}"),
104 None => String::from(""),
105 },
106 client_info.address,
107 client_info.transport,
108 client_info.consumer_groups_count
109 );
110 });
111 }
112 }
113
114 Ok(())
115 }
116}