Skip to main content

oauth_db_cli/commands/
config.rs

1use crate::cli::ConfigCommands;
2use crate::config::Config;
3use crate::error::{CliError, Result};
4use crate::output::{print_info, print_success};
5
6pub async fn execute(cmd: &ConfigCommands) -> Result<()> {
7    match cmd {
8        ConfigCommands::Set { key, value } => set_config(key, value).await,
9        ConfigCommands::Get { key } => get_config(key).await,
10        ConfigCommands::List => list_config().await,
11        ConfigCommands::Reset => reset_config().await,
12    }
13}
14
15async fn set_config(key: &str, value: &str) -> Result<()> {
16    let mut config = Config::load()?;
17
18    match key {
19        "server.url" => config.server.url = value.to_string(),
20        "output.format" => {
21            if !["table", "json", "yaml"].contains(&value) {
22                return Err(CliError::InvalidInput(format!(
23                    "Invalid output format: {}. Must be one of: table, json, yaml",
24                    value
25                )));
26            }
27            config.output.format = value.to_string();
28        }
29        "output.color" => {
30            config.output.color = value.parse().map_err(|_| {
31                CliError::InvalidInput("output.color must be 'true' or 'false'".to_string())
32            })?;
33        }
34        "log.level" => {
35            if !["error", "warn", "info", "debug"].contains(&value) {
36                return Err(CliError::InvalidInput(format!(
37                    "Invalid log level: {}. Must be one of: error, warn, info, debug",
38                    value
39                )));
40            }
41            config.log.level = value.to_string();
42        }
43        _ => {
44            return Err(CliError::InvalidInput(format!("Unknown config key: {}", key)));
45        }
46    }
47
48    config.save()?;
49    print_success(&format!("Set {} = {}", key, value));
50    Ok(())
51}
52
53async fn get_config(key: &str) -> Result<()> {
54    let config = Config::load()?;
55
56    let value = match key {
57        "server.url" => &config.server.url,
58        "output.format" => &config.output.format,
59        "output.color" => return Ok(print_info(&format!("{} = {}", key, config.output.color))),
60        "log.level" => &config.log.level,
61        _ => {
62            return Err(CliError::InvalidInput(format!("Unknown config key: {}", key)));
63        }
64    };
65
66    print_info(&format!("{} = {}", key, value));
67    Ok(())
68}
69
70async fn list_config() -> Result<()> {
71    let config = Config::load()?;
72
73    print_info("Current configuration:");
74    println!();
75    println!("server.url = {}", config.server.url);
76    println!("output.format = {}", config.output.format);
77    println!("output.color = {}", config.output.color);
78    println!("log.level = {}", config.log.level);
79    println!();
80    print_info(&format!("Accounts: {}", config.accounts.len()));
81
82    Ok(())
83}
84
85async fn reset_config() -> Result<()> {
86    let config = Config::default();
87    config.save()?;
88    print_success("Configuration reset to defaults");
89    Ok(())
90}