Skip to main content

iggy_cli/commands/binary_personal_access_tokens/
create_personal_access_token.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 iggy_common::Client;
23use iggy_common::PersonalAccessTokenExpiry;
24use iggy_common::create_personal_access_token::CreatePersonalAccessToken;
25use keyring::Entry;
26use tracing::{Level, event};
27
28pub struct CreatePersonalAccessTokenCmd {
29    create_token: CreatePersonalAccessToken,
30    token_expiry: Option<PersonalAccessTokenExpiry>,
31    quiet_mode: bool,
32    store_token: bool,
33    server_address: String,
34}
35
36impl CreatePersonalAccessTokenCmd {
37    pub fn new(
38        name: String,
39        pat_expiry: Option<PersonalAccessTokenExpiry>,
40        quiet_mode: bool,
41        store_token: bool,
42        server_address: String,
43    ) -> Self {
44        Self {
45            create_token: CreatePersonalAccessToken {
46                name,
47                expiry: match &pat_expiry {
48                    None => PersonalAccessTokenExpiry::NeverExpire,
49                    Some(value) => *value,
50                },
51            },
52            token_expiry: pat_expiry,
53            quiet_mode,
54            store_token,
55            server_address,
56        }
57    }
58}
59
60#[async_trait]
61impl CliCommand for CreatePersonalAccessTokenCmd {
62    fn explain(&self) -> String {
63        let expiry_text = match &self.token_expiry {
64            Some(value) => format!("token expire time: {value}"),
65            None => String::from("without token expire time"),
66        };
67        format!(
68            "create personal access token with name: {} and {}",
69            self.create_token.name, expiry_text
70        )
71    }
72
73    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
74        let token = client
75            .create_personal_access_token(&self.create_token.name, self.create_token.expiry)
76            .await
77            .with_context(|| {
78                format!(
79                    "Problem creating personal access token with name: {}",
80                    self.create_token.name
81                )
82            })?;
83
84        if self.store_token {
85            let server_address = format!("iggy:{}", self.server_address);
86            let entry = Entry::new(&server_address, &self.create_token.name)?;
87            entry.set_password(&token.token)?;
88            event!(target: PRINT_TARGET, Level::DEBUG,"Stored token under service: {} and name: {}", server_address,
89                    self.create_token.name);
90            event!(target: PRINT_TARGET, Level::INFO,
91                "Personal access token with name: {} and {} created",
92                self.create_token.name,
93                match &self.token_expiry {
94                    Some(value) => format!("token expire time: {value}"),
95                    None => String::from("without token expire time"),
96                },
97            );
98        } else if self.quiet_mode {
99            println!("{}", &token.token);
100        } else {
101            event!(target: PRINT_TARGET, Level::INFO,
102                "Personal access token with name: {} and {} created",
103                self.create_token.name,
104                match &self.token_expiry {
105                    Some(value) => format!("token expire time: {value}"),
106                    None => String::from("without token expire time"),
107                },
108            );
109            event!(target: PRINT_TARGET, Level::INFO,"Token: {}",
110                            &token.token);
111        }
112
113        Ok(())
114    }
115}