1
use clap::Args;
2

            
3
use mk_lib::secrets::{
4
  SecretBackend,
5
  SecretValueSource,
6
};
7

            
8
use crate::secrets::context::Context;
9

            
10
#[derive(Debug, Args)]
11
pub struct Doctor {}
12

            
13
impl Doctor {
14
  pub fn execute(&self, context: &Context) -> anyhow::Result<()> {
15
    let config = context.resolved_config();
16
    let active_config_path = context
17
      .active_config_path()
18
      .map(|path| path.to_string_lossy().to_string())
19
      .unwrap_or_else(|| String::from("<none>"));
20

            
21
    println!("Active config file: {active_config_path}");
22
    println!("Resolved backend: {}", render_backend(&config.backend));
23
    println!(
24
      "Resolved backend source: {}",
25
      render_source(config.backend_source)
26
    );
27
    println!("Resolved vault path: {}", config.vault_location.to_string_lossy());
28
    println!(
29
      "Resolved vault path source: {}",
30
      render_source(config.vault_location_source)
31
    );
32
    println!("Resolved keys path: {}", config.keys_location.to_string_lossy());
33
    println!(
34
      "Resolved keys path source: {}",
35
      render_source(config.keys_location_source)
36
    );
37
    println!("Resolved key name: {}", config.key_name);
38
    println!(
39
      "Resolved key name source: {}",
40
      render_source(config.key_name_source)
41
    );
42
    println!(
43
      "Resolved gpg key id: {}",
44
      config.gpg_key_id.as_deref().unwrap_or("<none>")
45
    );
46
    println!(
47
      "Resolved gpg key id source: {}",
48
      render_optional_source(config.gpg_key_id_source)
49
    );
50
    println!(
51
      "Vault metadata used: {}",
52
      if config.vault_meta_used { "yes" } else { "no" }
53
    );
54

            
55
    Ok(())
56
  }
57
}
58

            
59
fn render_backend(backend: &SecretBackend) -> &'static str {
60
  match backend {
61
    SecretBackend::BuiltInPgp => "pgp",
62
    SecretBackend::Gpg => "gpg",
63
  }
64
}
65

            
66
fn render_source(source: SecretValueSource) -> &'static str {
67
  match source {
68
    SecretValueSource::Cli => "cli",
69
    SecretValueSource::Task => "task",
70
    SecretValueSource::Root => "root",
71
    SecretValueSource::VaultMeta => "vault-meta",
72
    SecretValueSource::Default => "default",
73
  }
74
}
75

            
76
fn render_optional_source(source: Option<SecretValueSource>) -> &'static str {
77
  source.map(render_source).unwrap_or("<none>")
78
}