git_adr/cli/
config.rs

1//! Manage configuration.
2
3use anyhow::Result;
4use clap::{Args as ClapArgs, Subcommand};
5use colored::Colorize;
6
7use crate::core::{ConfigManager, Git};
8
9/// Arguments for the config command.
10#[derive(ClapArgs, Debug)]
11pub struct Args {
12    /// Config subcommand.
13    #[command(subcommand)]
14    pub command: ConfigCommand,
15}
16
17/// Config subcommands.
18#[derive(Subcommand, Debug)]
19pub enum ConfigCommand {
20    /// Get a configuration value.
21    Get {
22        /// Configuration key.
23        key: String,
24    },
25    /// Set a configuration value.
26    Set {
27        /// Configuration key.
28        key: String,
29        /// Configuration value.
30        value: String,
31    },
32    /// Unset a configuration value.
33    Unset {
34        /// Configuration key.
35        key: String,
36    },
37    /// List all configuration values.
38    List,
39}
40
41/// Known configuration keys with descriptions.
42const CONFIG_KEYS: &[(&str, &str)] = &[
43    ("initialized", "Whether git-adr is initialized (true/false)"),
44    ("prefix", "Prefix for ADR IDs (default: ADR-)"),
45    ("digits", "Number of digits in ADR IDs (default: 4)"),
46    ("template", "Default template name"),
47    ("format", "Default ADR format (nygard, madr, etc.)"),
48];
49
50/// Run the config command.
51///
52/// # Errors
53///
54/// Returns an error if config operation fails.
55pub fn run(args: Args) -> Result<()> {
56    let git = Git::new();
57    git.check_repository()?;
58
59    let config_manager = ConfigManager::new(git.clone());
60
61    match args.command {
62        ConfigCommand::Get { key } => {
63            if let Some(v) = config_manager.get(&key)? {
64                println!("{}", v);
65            } else {
66                eprintln!("{} Config key not set: adr.{}", "!".yellow(), key);
67                std::process::exit(1);
68            }
69        },
70        ConfigCommand::Set { key, value } => {
71            // Validate known keys
72            if !CONFIG_KEYS.iter().any(|(k, _)| *k == key) {
73                eprintln!(
74                    "{} Unknown config key: {}. Known keys are:",
75                    "!".yellow(),
76                    key
77                );
78                for (k, desc) in CONFIG_KEYS {
79                    eprintln!("  {} - {}", k.cyan(), desc);
80                }
81                eprintln!();
82                eprintln!("Setting anyway...");
83            }
84
85            config_manager.set(&key, &value)?;
86            eprintln!(
87                "{} Set adr.{} = {}",
88                "✓".green(),
89                key.cyan(),
90                value.yellow()
91            );
92        },
93        ConfigCommand::Unset { key } => {
94            // Use git directly to unset the key
95            git.config_unset(&format!("adr.{key}"), false)?;
96            eprintln!("{} Unset adr.{}", "✓".green(), key.cyan());
97        },
98        ConfigCommand::List => {
99            eprintln!("{} ADR Configuration:", "→".blue());
100            eprintln!();
101
102            let config = config_manager.load()?;
103
104            println!("{} = {}", "adr.initialized".cyan(), config.initialized);
105            println!("{} = {}", "adr.prefix".cyan(), config.prefix);
106            println!("{} = {}", "adr.digits".cyan(), config.digits);
107            println!("{} = {}", "adr.template".cyan(), config.template);
108            println!("{} = {}", "adr.format".cyan(), config.format);
109        },
110    }
111
112    Ok(())
113}