Skip to main content

iggy_cli/commands/binary_topics/
get_topic.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;
23use iggy_common::Client;
24use iggy_common::Identifier;
25use iggy_common::IggyExpiry;
26use tracing::{Level, event};
27
28pub struct GetTopicCmd {
29    stream_id: Identifier,
30    topic_id: Identifier,
31}
32
33impl GetTopicCmd {
34    pub fn new(stream_id: Identifier, topic_id: Identifier) -> Self {
35        Self {
36            stream_id,
37            topic_id,
38        }
39    }
40}
41
42#[async_trait]
43impl CliCommand for GetTopicCmd {
44    fn explain(&self) -> String {
45        format!(
46            "get topic with ID: {} from stream with ID: {}",
47            self.topic_id, self.stream_id
48        )
49    }
50
51    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
52        let topic = client
53            .get_topic(&self.stream_id, &self.topic_id)
54            .await
55            .with_context(|| {
56                format!(
57                    "Problem getting topic with ID: {} in stream {}",
58                    self.topic_id, self.stream_id
59                )
60            })?;
61
62        if topic.is_none() {
63            event!(target: PRINT_TARGET, Level::INFO, "Topic with ID: {} in stream {} was not found", self.topic_id, self.stream_id);
64            return Ok(());
65        }
66
67        let topic = topic.unwrap();
68        let mut table = Table::new();
69
70        table.set_header(vec!["Property", "Value"]);
71        table.add_row(vec!["Topic id", format!("{}", topic.id).as_str()]);
72        table.add_row(vec![
73            "Created",
74            topic.created_at.to_utc_string("%Y-%m-%d %H:%M:%S").as_str(),
75        ]);
76        table.add_row(vec!["Topic name", topic.name.as_str()]);
77        table.add_row(vec!["Topic size", format!("{}", topic.size).as_str()]);
78        table.add_row(vec![
79            "Compression",
80            topic.compression_algorithm.to_string().as_str(),
81        ]);
82        table.add_row(vec![
83            "Message expiry",
84            match topic.message_expiry {
85                IggyExpiry::NeverExpire => String::from("unlimited"),
86                IggyExpiry::ServerDefault => String::from("server_default"),
87                IggyExpiry::ExpireDuration(value) => format!("{value}"),
88            }
89            .as_str(),
90        ]);
91        table.add_row(vec![
92            "Max topic size",
93            format!("{}", topic.max_topic_size).as_str(),
94        ]);
95        table.add_row(vec![
96            "Topic message count",
97            format!("{}", topic.messages_count).as_str(),
98        ]);
99        table.add_row(vec![
100            "Partitions count",
101            format!("{}", topic.partitions_count).as_str(),
102        ]);
103
104        event!(target: PRINT_TARGET, Level::INFO,"{table}");
105
106        Ok(())
107    }
108}