1
use clap::Args;
2
use console::style;
3
use mk_lib::secrets::list_secret_paths;
4
use prettytable::format::consts;
5
use prettytable::{
6
  row,
7
  Table,
8
};
9

            
10
use crate::secrets::context::Context;
11

            
12
#[derive(Debug, Args)]
13
pub struct ListSecrets {
14
  #[arg(help = "Optional secret path prefix")]
15
  path: Option<String>,
16

            
17
  #[arg(short, long, help = "The path to the secret vault")]
18
  vault_location: Option<String>,
19

            
20
  #[arg(short, long, help = "Print plain output without headers")]
21
  plain: bool,
22
}
23

            
24
impl ListSecrets {
25
  pub fn execute(&self, context: &Context) -> anyhow::Result<()> {
26
    let path = self.path.as_deref();
27
    let mut cli_overrides = context.settings().clone();
28
    if let Some(vault_location) = &self.vault_location {
29
      cli_overrides.vault_location = Some(vault_location.clone());
30
    }
31
    let secret_config = context.resolve_with_settings(&cli_overrides);
32
    let secret_paths = list_secret_paths(path, &secret_config)?;
33
    if self.plain {
34
      for secret_path in secret_paths {
35
        println!("{}", secret_path);
36
      }
37
      return Ok(());
38
    }
39

            
40
    let mut table = Table::new();
41
    table.set_format(*consts::FORMAT_CLEAN);
42
    table.set_titles(row![Fbb->"Name"]);
43

            
44
    for secret_path in secret_paths {
45
      table.add_row(row![Fg->secret_path]);
46
    }
47

            
48
    let msg = style("Available secrets:").bold().cyan();
49
    println!();
50
    println!("{msg}");
51
    println!();
52
    table.printstd();
53

            
54
    Ok(())
55
  }
56
}