use clap::Subcommand; use std::path::Path; #[derive(Subcommand)] pub enum ConfigCommand { Init { #[arg(short, long)] force: bool, }, #[command(name = "config-show")] Show { #[arg(short, long)] section: Option, }, Edit { #[arg(short, long)] key: String, #[arg(short, long)] value: String, }, Validate, } pub fn handle_config_command(cmd: ConfigCommand) -> anyhow::Result<()> { match cmd { ConfigCommand::Init { force } => { let config_path = Path::new("config/markbase.toml"); if config_path.exists() && !force { println!("Configuration file already exists at config/markbase.toml"); println!("Use --force to overwrite"); return Ok(()); } let config = crate::config::MarkBaseConfig::default_config(); config.save(config_path)?; println!("✓ Configuration file created: config/markbase.toml"); println!("Default values:"); println!(" Server port: {}", config.server.port); println!(" PostgreSQL host: {}", config.postgresql.host); println!(" Test users: {}", config.test.users.join(", ")); } ConfigCommand::Show { section } => { let config_path = Path::new("config/markbase.toml"); if !config_path.exists() { println!( "Configuration file not found. Run 'markbase metadata config init' first." ); return Ok(()); } let config = crate::config::MarkBaseConfig::load(config_path)?; if let Some(s) = section { show_section(&config, &s); } else { println!("{}", toml::to_string_pretty(&config)?); } } ConfigCommand::Edit { key, value } => { let config_path = Path::new("config/markbase.toml"); if !config_path.exists() { println!( "Configuration file not found. Run 'markbase metadata config init' first." ); return Ok(()); } let mut config = crate::config::MarkBaseConfig::load(config_path)?; match config.get(&key) { Some(old_value) => { config.set(&key, &value)?; config.validate()?; config.save(config_path)?; println!("✓ Updated {}: {} → {}", key, old_value, value); } None => { println!("Invalid config key: {}", key); println!( "Valid keys: server.*, postgresql.*, authentication.*, test.*, logging.*" ); } } } ConfigCommand::Validate => { let config_path = Path::new("config/markbase.toml"); if !config_path.exists() { println!( "Configuration file not found. Run 'markbase metadata config init' first." ); return Ok(()); } let config = crate::config::MarkBaseConfig::load(config_path)?; match config.validate() { Ok(_) => { println!("✓ Configuration is valid"); } Err(e) => { println!("✗ Configuration validation failed: {}", e); } } } } Ok(()) } fn show_section(config: &crate::config::MarkBaseConfig, section: &str) { match section { "server" => println!("{}", toml::to_string_pretty(&config.server).unwrap()), "postgresql" => println!("{}", toml::to_string_pretty(&config.postgresql).unwrap()), "authentication" => println!("{}", toml::to_string_pretty(&config.authentication).unwrap()), "test" => println!("{}", toml::to_string_pretty(&config.test).unwrap()), "logging" => println!("{}", toml::to_string_pretty(&config.logging).unwrap()), _ => println!("Invalid section: {}. Valid sections: server, postgresql, authentication, test, logging", section), } }