1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use clap::{Subcommand, ValueEnum};
use std::path::PathBuf;
#[derive(Subcommand, Debug, Clone)]
pub enum ConfigAction {
/// Show current configuration
Show {
/// Show merged configuration from all sources
#[arg(long)]
merged: bool,
/// Show configuration source for each value
#[arg(long)]
sources: bool,
/// Output format
#[arg(long, default_value = "toml")]
format: ConfigFormat,
},
/// Initialize config file
Init {
/// Location (user, project, or path)
#[arg(default_value = "user")]
location: ConfigLocation,
/// Format
#[arg(long, default_value = "toml")]
format: ConfigFormat,
/// Overwrite existing file
#[arg(long)]
force: bool,
},
/// Get a configuration value
Get {
/// Key in dot notation (e.g., search.mode)
key: String,
},
/// Set a configuration value
Set {
/// Key in dot notation
key: String,
/// Value
value: String,
/// Configuration level
#[arg(long, default_value = "user")]
level: ConfigLocation,
},
/// Unset a configuration value
Unset {
/// Key in dot notation
key: String,
/// Configuration level
#[arg(long, default_value = "user")]
level: ConfigLocation,
},
/// List all configuration keys
List {
/// Filter by prefix
#[arg(long)]
prefix: Option<String>,
},
/// Validate configuration
Validate {
/// Path to config file
path: Option<PathBuf>,
},
/// Edit configuration file
Edit {
/// Configuration level
#[arg(default_value = "user")]
level: ConfigLocation,
},
/// Export configuration
Export {
/// Output format
#[arg(long, default_value = "toml")]
format: ConfigFormat,
/// Output file (stdout if not specified)
#[arg(long)]
output: Option<PathBuf>,
},
/// Import configuration
Import {
/// Input file
path: PathBuf,
/// Configuration level
#[arg(long, default_value = "user")]
level: ConfigLocation,
/// Merge with existing config
#[arg(long)]
merge: bool,
},
}
#[derive(Clone, ValueEnum, Debug)]
pub enum ConfigLocation {
System,
User,
Project,
}
#[derive(Clone, ValueEnum, Debug)]
pub enum ConfigFormat {
Toml,
Yaml,
Json,
}
use crate::error::{Result as RfgrepResult, RfgrepError};
pub async fn handle_config_action(action: ConfigAction) -> RfgrepResult<()> {
match action {
ConfigAction::Show {
merged,
sources: _,
format: _,
} => {
if merged {
let manager = crate::config::ConfigManager::new()?;
println!("{:#?}", manager.merged_config);
} else {
let config = crate::config::Config::load()?;
println!("{:#?}", config);
}
Ok(())
}
ConfigAction::Init {
location,
format: _,
force,
} => {
let path = match location {
ConfigLocation::User => dirs::config_dir()
.ok_or(RfgrepError::Other("No config directory found".to_string()))?
.join("rfgrep/config.toml"),
ConfigLocation::Project => PathBuf::from(".rfgreprc"),
ConfigLocation::System => PathBuf::from("/etc/rfgrep/config.toml"),
};
if path.exists() && !force {
println!(
"Config file already exists at {:?}. Use --force to overwrite.",
path
);
return Ok(());
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| {
RfgrepError::Other(format!("Failed to create directory: {}", e))
})?;
}
let config = crate::config::Config::default();
let content = toml::to_string_pretty(&config)
.map_err(|e| RfgrepError::Other(format!("Failed to serialize config: {}", e)))?;
std::fs::write(&path, content)
.map_err(|e| RfgrepError::Other(format!("Failed to write config file: {}", e)))?;
println!("Initialized default configuration at {:?}", path);
Ok(())
}
ConfigAction::Get { key } => {
let config = crate::config::Config::load()?;
let json =
serde_json::to_value(&config).map_err(|e| RfgrepError::Other(e.to_string()))?;
let mut current = &json;
for part in key.split('.') {
if let Some(val) = current.get(part) {
current = val;
} else {
return Err(RfgrepError::Other(format!("Key not found: {}", key)));
}
}
if let Some(s) = current.as_str() {
println!("{}", s);
} else {
println!("{}", current);
}
Ok(())
}
ConfigAction::List { prefix } => {
let config = crate::config::Config::load()?;
let json =
serde_json::to_value(&config).map_err(|e| RfgrepError::Other(e.to_string()))?;
fn print_keys(val: &serde_json::Value, prefix: &str, filter: Option<&str>) {
if let Some(obj) = val.as_object() {
for (k, v) in obj {
let new_key = if prefix.is_empty() {
k.clone()
} else {
format!("{}.{}", prefix, k)
};
if v.is_object() {
print_keys(v, &new_key, filter);
} else {
if let Some(f) = filter {
if !new_key.starts_with(f) {
continue;
}
}
println!("{} = {}", new_key, v);
}
}
}
}
print_keys(&json, "", prefix.as_deref());
Ok(())
}
_ => {
println!("Config action not fully implemented yet");
Ok(())
}
}
}