Skip to main content

iggy_cli/commands/binary_streams/
get_stream.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 tracing::{Level, event};
26
27pub struct GetStreamCmd {
28    stream_id: Identifier,
29}
30
31impl GetStreamCmd {
32    pub fn new(stream_id: Identifier) -> Self {
33        Self { stream_id }
34    }
35}
36
37#[async_trait]
38impl CliCommand for GetStreamCmd {
39    fn explain(&self) -> String {
40        format!("get stream with ID: {}", self.stream_id)
41    }
42
43    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
44        let stream = client
45            .get_stream(&self.stream_id)
46            .await
47            .with_context(|| format!("Problem getting stream with ID: {}", self.stream_id))?;
48
49        if stream.is_none() {
50            event!(target: PRINT_TARGET, Level::INFO, "Stream with ID: {} was not found", self.stream_id);
51            return Ok(());
52        }
53
54        let stream = stream.unwrap();
55        let mut table = Table::new();
56
57        table.set_header(vec!["Property", "Value"]);
58        table.add_row(vec!["Stream ID", format!("{}", stream.id).as_str()]);
59        table.add_row(vec!["Created", format!("{}", stream.created_at).as_str()]);
60        table.add_row(vec!["Stream name", stream.name.as_str()]);
61        table.add_row(vec!["Stream size", format!("{}", stream.size).as_str()]);
62        table.add_row(vec![
63            "Stream message count",
64            format!("{}", stream.messages_count).as_str(),
65        ]);
66        table.add_row(vec![
67            "Stream topics count",
68            format!("{}", stream.topics_count).as_str(),
69        ]);
70
71        event!(target: PRINT_TARGET, Level::INFO, "{table}");
72
73        Ok(())
74    }
75}