ngdp_client/commands/
config.rs

1use crate::{
2    ConfigCommands, OutputFormat,
3    config_manager::{ConfigError, ConfigManager},
4    output::{
5        OutputStyle, create_table, format_error, format_success, header_cell, print_section_header,
6        regular_cell,
7    },
8};
9
10pub async fn handle(
11    cmd: ConfigCommands,
12    format: OutputFormat,
13) -> Result<(), Box<dyn std::error::Error>> {
14    match cmd {
15        ConfigCommands::Show => show_config(format).await,
16        ConfigCommands::Set { key, value } => set_config(key, value, format).await,
17        ConfigCommands::Get { key } => get_config(key, format).await,
18        ConfigCommands::Reset { yes } => reset_config(yes, format).await,
19    }
20}
21
22async fn show_config(format: OutputFormat) -> Result<(), Box<dyn std::error::Error>> {
23    let config_manager = ConfigManager::new()?;
24    let config = config_manager.get_all();
25
26    match format {
27        OutputFormat::Json | OutputFormat::JsonPretty => {
28            let output = if matches!(format, OutputFormat::JsonPretty) {
29                serde_json::to_string_pretty(&config)?
30            } else {
31                serde_json::to_string(&config)?
32            };
33            println!("{output}");
34        }
35        _ => {
36            let style = OutputStyle::new();
37
38            print_section_header("Current Configuration", &style);
39
40            let mut table = create_table(&style);
41            table.set_header(vec![
42                header_cell("Key", &style),
43                header_cell("Value", &style),
44            ]);
45
46            let mut sorted_config: Vec<_> = config.iter().collect();
47            sorted_config.sort_by(|a, b| a.0.cmp(b.0));
48
49            for (key, value) in sorted_config {
50                table.add_row(vec![regular_cell(key), regular_cell(value)]);
51            }
52
53            println!("{table}");
54        }
55    }
56
57    Ok(())
58}
59
60async fn set_config(
61    key: String,
62    value: String,
63    format: OutputFormat,
64) -> Result<(), Box<dyn std::error::Error>> {
65    let mut config_manager = ConfigManager::new()?;
66    config_manager.set(key.clone(), value.clone())?;
67
68    match format {
69        OutputFormat::Json | OutputFormat::JsonPretty => {
70            let result = serde_json::json!({
71                "success": true,
72                "key": key,
73                "value": value,
74            });
75            println!("{}", serde_json::to_string(&result)?);
76        }
77        _ => {
78            let style = OutputStyle::new();
79            println!(
80                "{}",
81                format_success(&format!("✓ Set {key} = {value}"), &style)
82            );
83        }
84    }
85    Ok(())
86}
87
88async fn get_config(key: String, format: OutputFormat) -> Result<(), Box<dyn std::error::Error>> {
89    let config_manager = ConfigManager::new()?;
90    let value = config_manager.get(&key);
91
92    match format {
93        OutputFormat::Json | OutputFormat::JsonPretty => {
94            let result = match &value {
95                Ok(val) => serde_json::json!({
96                    "key": key,
97                    "value": val,
98                    "found": true,
99                }),
100                Err(_) => serde_json::json!({
101                    "key": key,
102                    "value": null,
103                    "found": false,
104                }),
105            };
106            println!("{}", serde_json::to_string(&result)?);
107        }
108        _ => {
109            let style = OutputStyle::new();
110            match value {
111                Ok(val) => println!("{val}"),
112                Err(ConfigError::KeyNotFound { key }) => {
113                    eprintln!(
114                        "{}",
115                        format_error(&format!("Configuration key '{key}' not found"), &style)
116                    );
117                    std::process::exit(1);
118                }
119                Err(e) => {
120                    eprintln!(
121                        "{}",
122                        format_error(&format!("Configuration error: {e}"), &style)
123                    );
124                    std::process::exit(1);
125                }
126            }
127        }
128    }
129
130    Ok(())
131}
132
133async fn reset_config(yes: bool, format: OutputFormat) -> Result<(), Box<dyn std::error::Error>> {
134    let style = OutputStyle::new();
135
136    if !yes {
137        eprintln!(
138            "{}",
139            format_error("Reset requires confirmation. Use --yes to confirm.", &style)
140        );
141        std::process::exit(1);
142    }
143
144    let mut config_manager = ConfigManager::new()?;
145    config_manager.reset()?;
146
147    match format {
148        OutputFormat::Json | OutputFormat::JsonPretty => {
149            let result = serde_json::json!({
150                "success": true,
151                "message": "Configuration reset to defaults",
152            });
153            println!("{}", serde_json::to_string(&result)?);
154        }
155        _ => {
156            println!(
157                "{}",
158                format_success("✓ Configuration reset to defaults", &style)
159            );
160        }
161    }
162
163    Ok(())
164}