mz/command/
app_password.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Implementation of the `mz app-password` command.
17//!
18//! Consult the user-facing documentation for details.
19
20use mz_frontegg_client::client::app_password::CreateAppPasswordRequest;
21use serde::{Deserialize, Serialize};
22use tabled::Tabled;
23
24use crate::context::ProfileContext;
25use crate::error::Error;
26
27/// Represents the args to create an app-password
28pub struct CreateArgs<'a> {
29    /// Represents the description used for the app-password.
30    /// The description tends to be related to its usage.
31    /// E.g.: `production`, `staging`, `bi_tools`, `ci_cd`, etc.
32    pub description: &'a str,
33}
34
35/// Creates and shows a new app-password for the profile.
36pub async fn create(
37    cx: &ProfileContext,
38    params: CreateAppPasswordRequest<'_>,
39) -> Result<(), Error> {
40    let app_password = cx.admin_client().create_app_password(params).await?;
41    println!("{}", app_password);
42    Ok(())
43}
44
45/// Lists all the profile app-password descriptions available
46/// and their creation date.
47pub async fn list(cx: &ProfileContext) -> Result<(), Error> {
48    let loading_spinner = cx
49        .output_formatter()
50        .loading_spinner("Retrieving app-passwords...");
51
52    #[derive(Deserialize, Serialize, Tabled)]
53    pub struct AppPassword {
54        #[tabled(rename = "Name")]
55        description: String,
56        #[tabled(rename = "Created At")]
57        created_at: String,
58    }
59
60    let passwords = cx.admin_client().list_app_passwords().await?;
61    loading_spinner.finish_and_clear();
62
63    let output_formatter = cx.output_formatter();
64    output_formatter.output_table(passwords.iter().map(|x| AppPassword {
65        description: x.description.clone(),
66        created_at: x.created_at.clone(),
67    }))?;
68
69    Ok(())
70}