iggy_cli/commands/binary_consumer_groups/
get_consumer_groups.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 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}