Skip to main content

iggy_cli/commands/binary_users/
get_users.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 GetUsersOutput {
27    Table,
28    List,
29}
30
31pub struct GetUsersCmd {
32    output: GetUsersOutput,
33}
34
35impl GetUsersCmd {
36    pub fn new(output: GetUsersOutput) -> Self {
37        GetUsersCmd { output }
38    }
39}
40
41impl Default for GetUsersCmd {
42    fn default() -> Self {
43        GetUsersCmd {
44            output: GetUsersOutput::Table,
45        }
46    }
47}
48
49#[async_trait]
50impl CliCommand for GetUsersCmd {
51    fn explain(&self) -> String {
52        let mode = match self.output {
53            GetUsersOutput::Table => "table",
54            GetUsersOutput::List => "list",
55        };
56        format!("list users in {mode} mode")
57    }
58
59    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
60        let users = client
61            .get_users()
62            .await
63            .with_context(|| String::from("Problem getting list of users"))?;
64
65        if users.is_empty() {
66            event!(target: PRINT_TARGET, Level::INFO, "No users found!");
67            return Ok(());
68        }
69
70        match self.output {
71            GetUsersOutput::Table => {
72                let mut table = Table::new();
73
74                table.set_header(vec!["ID", "Created", "Status", "Username"]);
75
76                users.iter().for_each(|user| {
77                    table.add_row(vec![
78                        format!("{}", user.id),
79                        user.created_at.to_local_string("%Y-%m-%d %H:%M:%S"),
80                        user.status.clone().to_string(),
81                        user.username.clone(),
82                    ]);
83                });
84
85                event!(target: PRINT_TARGET, Level::INFO, "{table}");
86            }
87            GetUsersOutput::List => {
88                users.iter().for_each(|user| {
89                    event!(target: PRINT_TARGET, Level::INFO,
90                        "{}|{}|{}|{}",
91                        user.id,
92                        user.created_at.to_local_string("%Y-%m-%d %H:%M:%S"),
93                        user.status.clone().to_string(),
94                        user.username.clone(),
95                    );
96                });
97            }
98        }
99
100        Ok(())
101    }
102}