iggy_cli/commands/binary_personal_access_tokens/
get_personal_access_tokens.rs1use 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}