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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
use crate::atuin_client::settings::Settings;
use clap::{Args, Subcommand, ValueEnum};
use eyre::Result;
use toml_edit::{Document, DocumentMut, Item, Table, TableLike, Value};
#[derive(Subcommand, Debug)]
#[command(infer_subcommands = true)]
pub enum Cmd {
/// Get a configuration value from your config.toml file
/// or after defaults and overrides are applied
#[command()]
Get(GetCmd),
/// Set a configuration value in your config.toml file
#[command()]
Set(SetCmd),
/// Print all configuration values from your config.toml file
/// in TOML format
///
/// If a key is provided, only print the value of that key and all its children
#[command()]
Print(PrintCmd),
}
impl Cmd {
pub async fn run(self, settings: &Settings) -> Result<()> {
match self {
Self::Get(get) => get.run(settings).await,
Self::Set(set) => set.run(settings).await,
Self::Print(print) => print.run(settings).await,
}
}
}
/// Get a configuration value from your config.toml file,
/// or optionally the effective value after defaults and overrides are applied.
#[derive(Args, Debug)]
pub struct GetCmd {
/// The configuration key to get
pub key: String,
/// Print the value after defaults and overrides are applied
#[arg(long, short)]
pub resolved: bool,
/// Print both the config file value and the resolved value
#[arg(long, short)]
pub verbose: bool,
}
impl GetCmd {
pub async fn run(&self, _settings: &Settings) -> Result<()> {
let key = self.key.trim();
if key.is_empty() || key.contains(char::is_whitespace) {
eyre::bail!("Config key must be non-empty and must not contain whitespace");
}
if self.verbose {
println!("Config file:");
self.print_current_value(key, " ").await?;
println!("\nResolved:");
Self::print_effective_value(key, " ");
return Ok(());
}
if self.resolved {
Self::print_effective_value(key, "");
} else {
self.print_current_value(key, "").await?;
}
Ok(())
}
async fn print_current_value(&self, key: &str, prefix: &str) -> Result<()> {
let config_file = Settings::get_config_path()?;
let config_str = tokio::fs::read_to_string(&config_file).await?;
let doc = config_str.parse::<Document<_>>()?;
let current = get_deep_key(&doc, key);
match current {
Some(item) if item.is_table() || item.is_inline_table() => {
let table = item
.as_table_like()
.expect("is_table()/is_inline_table() but no table");
println!("{prefix}[{key}]");
dump_table(table, prefix, &mut vec![key.to_string()])?;
}
Some(item) => {
let val = item.to_string();
let val = val.trim().trim_matches('"');
println!("{prefix}{val}");
}
None => {
println!("{prefix}(not set in config file)");
}
}
Ok(())
}
fn print_effective_value(key: &str, prefix: &str) {
match Settings::get_config_value(key) {
Ok(value) => {
for line in value.lines() {
println!("{prefix}{line}");
}
}
Err(_) => {
println!("{prefix}(unknown key)");
}
}
}
}
#[derive(Args, Debug)]
pub struct SetCmd {
/// The configuration key to set
pub key: String,
/// The value to set
pub value: String,
/// Store value as an explicit type
#[arg(long = "type", short, value_enum, default_value_t = ValueType::Auto, value_name = "TYPE")]
pub the_type: ValueType,
}
#[derive(ValueEnum, Debug, Clone, PartialEq, Eq)]
pub enum ValueType {
/// Automatically determine the type of the value
Auto,
/// Store value as a string
String,
/// Store value as a boolean
Boolean,
/// Store value as an integer
Integer,
/// Store the value as a float
Float,
}
impl SetCmd {
pub async fn run(self, _settings: &Settings) -> Result<()> {
let key = self.key.trim();
if key.is_empty() || key.contains(char::is_whitespace) {
eyre::bail!("Config key must be non-empty and must not contain whitespace");
}
let config_file = Settings::get_config_path()?;
let config_str = tokio::fs::read_to_string(&config_file).await?;
let mut doc: DocumentMut = config_str.parse()?;
// When using auto type detection, try to match the existing value's type
// so we don't accidentally change e.g. "300" (string) to 300 (integer)
let existing_type = detect_existing_type(&doc, key);
let value = self.parse_value(existing_type.as_ref())?;
set_deep_key(&mut doc, key, value)?;
tokio::fs::write(&config_file, doc.to_string()).await?;
Ok(())
}
fn parse_value(&self, existing_type: Option<&ValueType>) -> Result<Value> {
let raw = &self.value;
// Explicit --type takes priority, then existing value type, then auto-detect
let effective_type = if self.the_type != ValueType::Auto {
&self.the_type
} else if let Some(existing) = existing_type {
existing
} else {
&ValueType::Auto
};
match effective_type {
ValueType::String => Ok(Value::from(raw.as_str())),
ValueType::Boolean => {
let b: bool = raw
.parse()
.map_err(|_| eyre::eyre!("invalid boolean value: {raw}"))?;
Ok(Value::from(b))
}
ValueType::Integer => {
let i: i64 = raw
.parse()
.map_err(|_| eyre::eyre!("invalid integer value: {raw}"))?;
Ok(Value::from(i))
}
ValueType::Float => {
let f: f64 = raw
.parse()
.map_err(|_| eyre::eyre!("invalid float value: {raw}"))?;
Ok(Value::from(f))
}
ValueType::Auto => {
if raw == "true" || raw == "false" {
return Ok(Value::from(raw == "true"));
}
if let Ok(i) = raw.parse::<i64>() {
return Ok(Value::from(i));
}
if let Ok(f) = raw.parse::<f64>() {
return Ok(Value::from(f));
}
Ok(Value::from(raw.as_str()))
}
}
}
}
#[derive(Args, Debug)]
pub struct PrintCmd {
/// Print the value of a specific key and all its children
pub key: Option<String>,
}
impl PrintCmd {
pub async fn run(&self, _settings: &Settings) -> Result<()> {
let config_file = Settings::get_config_path()?;
let config_str = tokio::fs::read_to_string(&config_file).await?;
let doc = config_str.parse::<Document<_>>()?;
if let Some(key) = &self.key {
let current = get_deep_key(&doc, key);
if let Some(current) = current {
if current.is_table() || current.is_inline_table() {
println!("[{key}]");
dump_table(
current
.as_table_like()
.expect("is_table()/is_inline_table() but no table"),
"",
&mut vec![key.clone()],
)?;
} else {
println!("{}", current.to_string().trim().trim_matches('"'));
}
} else {
println!("key not found");
}
} else {
dump_table(doc.as_table(), "", &mut Vec::new())?;
}
Ok(())
}
}
fn dump_table(table: &dyn TableLike, prefix: &str, stack: &mut Vec<String>) -> Result<()> {
for (key, value) in table.iter() {
if value.is_table() || value.is_inline_table() {
stack.push(key.to_string());
let table = value
.as_table_like()
.expect("is_table()/is_inline_table() but no table");
println!("\n{}[{}]", prefix, stack.join("."));
dump_table(table, prefix, stack)?;
stack.pop();
} else {
println!("{prefix}{key} = {value}");
}
}
Ok(())
}
fn get_deep_key<'doc>(doc: &'doc Document<String>, key: &str) -> Option<&'doc Item> {
let parts = key.split('.');
let mut current: Option<&Item> = Some(doc.as_item());
for part in parts {
current = current
.and_then(|item| item.as_table_like())
.and_then(|table| table.get(part));
}
current
}
/// Detect the TOML type of an existing key in the document, so `set` with auto
/// type detection preserves the original type rather than guessing from the value string.
fn detect_existing_type(doc: &DocumentMut, key: &str) -> Option<ValueType> {
let parts: Vec<&str> = key.split('.').collect();
let mut current: &dyn TableLike = doc.as_table();
for &part in &parts[..parts.len().saturating_sub(1)] {
current = current.get(part)?.as_table_like()?;
}
let last = parts.last()?;
let v = current.get(last)?.as_value()?;
if v.is_str() {
Some(ValueType::String)
} else if v.is_bool() {
Some(ValueType::Boolean)
} else if v.is_integer() {
Some(ValueType::Integer)
} else if v.is_float() {
Some(ValueType::Float)
} else {
None
}
}
fn set_deep_key(doc: &mut DocumentMut, key: &str, value: Value) -> Result<()> {
let parts: Vec<&str> = key.split('.').collect();
if parts.is_empty() {
eyre::bail!("empty config key");
}
let mut current: &mut dyn TableLike = doc.as_table_mut();
// Navigate/create intermediate tables
for &part in &parts[..parts.len() - 1] {
if !current.contains_key(part) {
current.insert(part, Item::Table(Table::new()));
}
current = current
.get_mut(part)
.expect("just inserted or already exists")
.as_table_like_mut()
.ok_or_else(|| eyre::eyre!("'{}' exists but is not a table", part))?;
}
let last = *parts.last().unwrap();
// Don't silently overwrite a table with a scalar value
if let Some(existing) = current.get(last)
&& (existing.is_table() || existing.is_inline_table())
{
eyre::bail!(
"'{}' is a table; use a dotted key like '{}.key' to set a value within it",
key,
key
);
}
current.insert(last, Item::Value(value));
Ok(())
}
|