Skip to main content

iggy_cli/commands/binary_personal_access_tokens/
get_personal_access_tokens.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 GetPersonalAccessTokensOutput {
27    Table,
28    List,
29}
30
31pub struct GetPersonalAccessTokensCmd {
32    output: GetPersonalAccessTokensOutput,
33}
34
35impl GetPersonalAccessTokensCmd {
36    pub fn new(output: GetPersonalAccessTokensOutput) -> Self {
37        Self { output }
38    }
39}
40
41#[async_trait]
42impl CliCommand for GetPersonalAccessTokensCmd {
43    fn explain(&self) -> String {
44        let mode = match self.output {
45            GetPersonalAccessTokensOutput::Table => "table",
46            GetPersonalAccessTokensOutput::List => "list",
47        };
48        format!("list personal access tokens in {mode} mode")
49    }
50
51    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
52        let tokens = client
53            .get_personal_access_tokens()
54            .await
55            .with_context(|| String::from("Problem getting list of personal access tokens"))?;
56
57        match self.output {
58            GetPersonalAccessTokensOutput::Table => {
59                let mut table = Table::new();
60
61                table.set_header(vec!["Name", "Token Expiry Time"]);
62
63                tokens.iter().for_each(|token| {
64                    table.add_row(vec![
65                        format!("{}", token.name.clone()),
66                        match token.expiry_at {
67                            None => String::from("unlimited"),
68                            Some(value) => value.to_local_string("%Y-%m-%d %H:%M:%S"),
69                        },
70                    ]);
71                });
72
73                event!(target: PRINT_TARGET, Level::INFO, "{table}");
74            }
75            GetPersonalAccessTokensOutput::List => {
76                tokens.iter().for_each(|token| {
77                    event!(target: PRINT_TARGET, Level::INFO,
78                        "{}|{}",
79                        token.name,
80                        match token.expiry_at {
81                            None => String::from("unlimited"),
82                            Some(value) => value.to_local_string("%Y-%m-%d %H:%M:%S"),
83                        },
84                    );
85                });
86            }
87        }
88
89        Ok(())
90    }
91}