Skip to main content

iggy_cli/commands/binary_consumer_groups/
get_consumer_groups.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 std::fmt::{self, Display, Formatter};
26use tracing::{Level, event};
27
28pub enum GetConsumerGroupsOutput {
29    Table,
30    List,
31}
32
33impl Display for GetConsumerGroupsOutput {
34    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
35        match self {
36            GetConsumerGroupsOutput::Table => write!(f, "table"),
37            GetConsumerGroupsOutput::List => write!(f, "list"),
38        }?;
39
40        Ok(())
41    }
42}
43
44pub struct GetConsumerGroupsCmd {
45    stream_id: Identifier,
46    topic_id: Identifier,
47    output: GetConsumerGroupsOutput,
48}
49
50impl GetConsumerGroupsCmd {
51    pub fn new(
52        stream_id: Identifier,
53        topic_id: Identifier,
54        output: GetConsumerGroupsOutput,
55    ) -> Self {
56        Self {
57            stream_id,
58            topic_id,
59            output,
60        }
61    }
62}
63
64#[async_trait]
65impl CliCommand for GetConsumerGroupsCmd {
66    fn explain(&self) -> String {
67        format!(
68            "list consumer groups for stream with ID: {} and topic with ID: {} in {} mode",
69            self.stream_id, self.topic_id, self.output
70        )
71    }
72
73    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
74        let consumer_groups = client
75            .get_consumer_groups(&self.stream_id, &self.topic_id)
76            .await
77            .with_context(|| {
78                format!(
79                    "Problem getting consumer groups for stream with ID: {} and topic with ID: {}",
80                    self.stream_id, self.topic_id
81                )
82            })?;
83
84        match self.output {
85            GetConsumerGroupsOutput::Table => {
86                let mut table = Table::new();
87                table.set_header(vec!["ID", "Name", "Partitions Count", "Members Count"]);
88                consumer_groups.iter().for_each(|group| {
89                    table.add_row(vec![
90                        format!("{}", group.id),
91                        group.name.clone(),
92                        format!("{}", group.partitions_count),
93                        format!("{}", group.members_count),
94                    ]);
95                });
96
97                event!(target: PRINT_TARGET, Level::INFO, "{table}");
98            }
99            GetConsumerGroupsOutput::List => {
100                consumer_groups.iter().for_each(|group| {
101                    event!(target: PRINT_TARGET, Level::INFO,
102                        "{}|{}|{}|{}",
103                        group.id,
104                        group.name,
105                        group.partitions_count,
106                        group.members_count,
107                    );
108                });
109            }
110        }
111
112        Ok(())
113    }
114}