Skip to main content

iggy_cli/commands/binary_users/
get_user.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 GetUserCmd {
28    user_id: Identifier,
29}
30
31impl GetUserCmd {
32    pub fn new(user_id: Identifier) -> Self {
33        Self { user_id }
34    }
35}
36
37#[async_trait]
38impl CliCommand for GetUserCmd {
39    fn explain(&self) -> String {
40        format!("get user with ID: {}", self.user_id)
41    }
42
43    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
44        let user = client
45            .get_user(&self.user_id)
46            .await
47            .with_context(|| format!("Problem getting user with ID: {}", self.user_id))?;
48
49        if user.is_none() {
50            event!(
51                target: PRINT_TARGET,
52                Level::INFO,
53                "User with ID: {} was not found",
54                self.user_id
55            );
56            return Ok(());
57        }
58
59        let user = user.unwrap();
60        let mut table = Table::new();
61
62        table.set_header(vec!["Property", "Value"]);
63        table.add_row(vec!["User ID", format!("{}", user.id).as_str()]);
64        table.add_row(vec![
65            "Created",
66            user.created_at
67                .to_local_string("%Y-%m-%d %H:%M:%S")
68                .as_str(),
69        ]);
70        table.add_row(vec!["Status", format!("{}", user.status).as_str()]);
71        table.add_row(vec!["Username", user.username.as_str()]);
72
73        if let Some(permissions) = user.permissions {
74            let global_permissions: Table = permissions.global.into();
75            table.add_row(vec!["Global", format!("{global_permissions}").as_str()]);
76
77            if let Some(streams) = permissions.streams {
78                streams.iter().for_each(|(stream_id, stream_permissions)| {
79                    let stream_permissions: Table = stream_permissions.into();
80                    table.add_row(vec![
81                        format!("Stream: {stream_id}").as_str(),
82                        format!("{stream_permissions}").as_str(),
83                    ]);
84                });
85            }
86        };
87
88        event!(target: PRINT_TARGET, Level::INFO, "{table}");
89
90        Ok(())
91    }
92}