1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use clap::{arg, Args};

use colorful::Colorful;
use ockam::Context;

use crate::{fmt_log, terminal::OckamColor, util::node_rpc, CommandGlobalOpts};

use super::CredentialOutput;

#[derive(Clone, Debug, Args)]
pub struct ListCommand {
    /// Name of the Vault from which to retrieve the credentials
    #[arg(value_name = "VAULT_NAME")]
    pub vault: Option<String>,
}

impl ListCommand {
    pub fn run(self, opts: CommandGlobalOpts) {
        node_rpc(run_impl, (opts, self));
    }
}

async fn run_impl(
    _ctx: Context,
    (opts, cmd): (CommandGlobalOpts, ListCommand),
) -> miette::Result<()> {
    opts.terminal
        .write_line(&fmt_log!("Listing Credentials...\n"))?;

    let vault_name = opts
        .state
        .get_named_vault_or_default(&cmd.vault)
        .await?
        .name();
    let mut credentials: Vec<CredentialOutput> = Vec::new();

    for credential in opts.state.get_credentials().await? {
        let credential_output = CredentialOutput::new(credential).await;
        credentials.push(credential_output);
    }

    let list = opts.terminal.build_list(
        &credentials,
        "Credentials",
        &format!(
            "No Credentials found for vault: {}",
            vault_name.color(OckamColor::PrimaryResource.color())
        ),
    )?;

    opts.terminal.stdout().plain(list).write_line()?;

    Ok(())
}