1
use std::fs;
2
use std::path::Path;
3

            
4
use clap::Args;
5

            
6
use crate::secrets::context::Context;
7

            
8
#[derive(Debug, Args)]
9
pub struct InitVault {
10
  #[arg(short, long, help = "The path to the secret vault")]
11
  vault_location: Option<String>,
12

            
13
  #[arg(short, long, help = "The key name")]
14
  key_name: Option<String>,
15
}
16

            
17
impl InitVault {
18
  pub fn execute(&self, context: &Context) -> anyhow::Result<()> {
19
    let vault_location: &str = &self
20
      .vault_location
21
      .clone()
22
      .unwrap_or_else(|| context.vault_location());
23
    let key_name: &str = &self.key_name.clone().unwrap_or_else(|| context.key_name());
24

            
25
    assert!(!vault_location.is_empty(), "Vault location must be provided");
26
    assert!(!key_name.is_empty(), "Key name must be provided");
27

            
28
    let path = Path::new(vault_location);
29
    if path.exists() {
30
      println!("Vault already exists at {vault_location}");
31
    } else {
32
      fs::create_dir_all(path)?;
33
      println!("Vault created at {vault_location}");
34
    }
35
    Ok(())
36
  }
37
}