1
use clap::{
2
  Args,
3
  Subcommand,
4
};
5
use context::Context;
6
use key::KEY_LOCATION_HELP;
7

            
8
mod context;
9
mod key;
10
mod utils;
11
mod vault;
12

            
13
/// The struct that represents the secrets command
14
#[derive(Debug, Args)]
15
pub struct Secrets {
16
  #[command(subcommand)]
17
  command: Option<SecretsCommand>,
18

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

            
22
  #[arg(long, help = KEY_LOCATION_HELP)]
23
  keys_location: Option<String>,
24

            
25
  #[arg(long, help = "The key name")]
26
  key_name: Option<String>,
27
}
28

            
29
/// The available subcommands for the secrets command
30
#[derive(Debug, Subcommand)]
31
enum SecretsCommand {
32
  /// Access private keys using this subcommand
33
  #[command(visible_aliases = ["k"], arg_required_else_help = true, about = "Access private keys")]
34
  Key(key::Key),
35

            
36
  /// Access secret stores using this subcommand
37
  #[command(visible_aliases = ["v"], arg_required_else_help = true, about = "Access secret vault")]
38
  Vault(vault::Vault),
39

            
40
  /// List private keys
41
  #[command(visible_aliases = ["K"], about = "List private keys")]
42
  ListKeys(key::ListKeys),
43

            
44
  /// Initialize a new secret store
45
  #[command(visible_aliases = ["init"], about = "Initialize a new secret vault")]
46
  InitVault(vault::InitVault),
47

            
48
  /// Export a secret store
49
  #[command(visible_aliases = ["export", "e"], about = "Export secrets to file")]
50
  ExportSecrets(vault::ExportSecrets),
51
}
52

            
53
impl Secrets {
54
  pub fn execute(&self) -> anyhow::Result<()> {
55
    let mut context = Context::new();
56
    if let Some(keys_location) = &self.keys_location {
57
      context.set_keys_location(keys_location);
58
    }
59

            
60
    if let Some(vault_location) = &self.vault_location {
61
      context.set_vault_location(vault_location);
62
    }
63

            
64
    match &self.command {
65
      Some(SecretsCommand::Key(key)) => key.execute(&mut context),
66
      Some(SecretsCommand::Vault(vault)) => vault.execute(&mut context),
67
      Some(SecretsCommand::ListKeys(list_keys)) => list_keys.execute(&context),
68
      Some(SecretsCommand::InitVault(init_store)) => init_store.execute(&context),
69
      Some(SecretsCommand::ExportSecrets(export_secrets)) => export_secrets.execute(&context),
70
      None => Err(anyhow::anyhow!("No subcommand provided")),
71
    }
72
  }
73
}