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
use clap::ArgMatches;
use crate::core::*;
use std::fs::create_dir_all;
use std::path::Path;

#[derive(Debug)]
pub struct InitArgs {
  pub path: String,
}

impl InitArgs {
  fn to_vault(&self, config: &Config) -> std::io::Result<Vault> {
    let path = std::fs::canonicalize(self.path.to_owned())?.into_os_string().into_string().unwrap();
    Ok(Vault::new(config, path))
  }
}

pub fn run(vault: &Vault, args: &InitArgs) -> Result<()> {
  preflight_check(args)?;
  bootstrap(vault, &args)
}

pub fn match_args(matches: &ArgMatches) -> Option<InitArgs> {
  if let Some(matches) = matches.subcommand_matches("init") {
    let input = matches.value_of("PATH").unwrap();
    return Some(InitArgs {
      path: input.to_owned(),
    });
  }
  None
}

fn preflight_check(args: &InitArgs) -> Result<()> {
  let path = args.path.to_owned();
  if !Path::new(&path).exists() {
    create_dir_all(&path)?;
  }
  Ok(())
}

fn bootstrap(vault: &Vault, args: &InitArgs) -> Result<()> {
  args.to_vault(&vault.config)?.bootstrap()
}