Skip to main content

iggy_cli/commands/binary_client/
get_client.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::{Table, presets::ASCII_NO_BORDERS};
23use iggy_common::Client;
24use tracing::{Level, event};
25
26pub struct GetClientCmd {
27    client_id: u32,
28}
29
30impl GetClientCmd {
31    pub fn new(client_id: u32) -> Self {
32        Self { client_id }
33    }
34}
35
36#[async_trait]
37impl CliCommand for GetClientCmd {
38    fn explain(&self) -> String {
39        format!("get client with ID: {}", self.client_id)
40    }
41
42    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
43        let client_details = client
44            .get_client(self.client_id)
45            .await
46            .with_context(|| format!("Problem getting client with ID: {}", self.client_id))?;
47
48        if client_details.is_none() {
49            event!(target: PRINT_TARGET, Level::INFO, "Client with ID: {} was not found", self.client_id);
50            return Ok(());
51        }
52
53        let client_details = client_details.unwrap();
54        let mut table = Table::new();
55
56        table.set_header(vec!["Property", "Value"]);
57        table.add_row(vec![
58            "Client ID",
59            format!("{}", client_details.client_id).as_str(),
60        ]);
61
62        let user = match client_details.user_id {
63            Some(user_id) => format!("{user_id}"),
64            None => String::from("None"),
65        };
66        table.add_row(vec!["User ID", user.as_str()]);
67
68        table.add_row(vec!["Address", client_details.address.as_str()]);
69        table.add_row(vec!["Transport", client_details.transport.as_str()]);
70        table.add_row(vec![
71            "Consumer Groups Count",
72            format!("{}", client_details.consumer_groups_count).as_str(),
73        ]);
74
75        if client_details.consumer_groups_count > 0 {
76            let mut consumer_groups = Table::new();
77            consumer_groups.load_preset(ASCII_NO_BORDERS);
78            consumer_groups.set_header(vec!["Stream ID", "Topic ID", "Consumer Group ID"]);
79            for consumer_group in client_details.consumer_groups {
80                consumer_groups.add_row(vec![
81                    format!("{}", consumer_group.stream_id).as_str(),
82                    format!("{}", consumer_group.topic_id).as_str(),
83                    format!("{}", consumer_group.group_id).as_str(),
84                ]);
85            }
86
87            table.add_row(vec![
88                "Consumer Groups Details",
89                consumer_groups.to_string().as_str(),
90            ]);
91        }
92
93        event!(target: PRINT_TARGET, Level::INFO, "{table}");
94
95        Ok(())
96    }
97}