Skip to main content

iggy_cli/commands/binary_streams/
get_streams.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 tracing::{Level, event};
25
26pub enum GetStreamsOutput {
27    Table,
28    List,
29}
30
31pub struct GetStreamsCmd {
32    output: GetStreamsOutput,
33}
34
35impl GetStreamsCmd {
36    pub fn new(output: GetStreamsOutput) -> Self {
37        GetStreamsCmd { output }
38    }
39}
40
41impl Default for GetStreamsCmd {
42    fn default() -> Self {
43        GetStreamsCmd {
44            output: GetStreamsOutput::Table,
45        }
46    }
47}
48
49#[async_trait]
50impl CliCommand for GetStreamsCmd {
51    fn explain(&self) -> String {
52        let mode = match self.output {
53            GetStreamsOutput::Table => "table",
54            GetStreamsOutput::List => "list",
55        };
56        format!("list streams in {mode} mode")
57    }
58
59    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
60        let streams = client
61            .get_streams()
62            .await
63            .with_context(|| String::from("Problem getting list of streams"))?;
64
65        if streams.is_empty() {
66            event!(target: PRINT_TARGET, Level::INFO, "No streams found!");
67            return Ok(());
68        }
69
70        match self.output {
71            GetStreamsOutput::Table => {
72                let mut table = Table::new();
73
74                table.set_header(vec![
75                    "ID", "Created", "Name", "Size (B)", "Messages", "Topics",
76                ]);
77
78                streams.iter().for_each(|stream| {
79                    table.add_row(vec![
80                        format!("{}", stream.id),
81                        format!("{}", stream.created_at),
82                        stream.name.clone(),
83                        format!("{}", stream.size),
84                        format!("{}", stream.messages_count),
85                        format!("{}", stream.topics_count),
86                    ]);
87                });
88
89                event!(target: PRINT_TARGET, Level::INFO, "{table}");
90            }
91            GetStreamsOutput::List => {
92                streams.iter().for_each(|stream| {
93                    event!(target: PRINT_TARGET, Level::INFO,
94                        "{}|{}|{}|{}|{}|{}",
95                        stream.id,
96                        stream.created_at,
97                        stream.name,
98                        stream.size,
99                        stream.messages_count,
100                        stream.topics_count
101                    );
102                });
103            }
104        }
105
106        Ok(())
107    }
108}