-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathunset.rs
More file actions
65 lines (57 loc) · 1.93 KB
/
Copy pathunset.rs
File metadata and controls
65 lines (57 loc) · 1.93 KB
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
use eyre::{Result, eyre};
use toml_edit::DocumentMut;
use crate::config::settings::SettingsFile;
use crate::{config, file};
/// Clears a setting
///
/// This modifies the contents of ~/.config/mise/config.toml
#[derive(Debug, clap::Args)]
#[clap(visible_aliases = ["rm", "remove", "delete", "del"], after_long_help = AFTER_LONG_HELP, verbatim_doc_comment)]
pub struct SettingsUnset {
/// The setting to remove
pub key: String,
/// Use the local config file instead of the global one
#[clap(long, short)]
pub local: bool,
}
impl SettingsUnset {
pub fn run(self) -> Result<()> {
unset(&self.key, self.local)
}
}
pub fn unset(mut key: &str, local: bool) -> Result<()> {
let path = if local {
config::local_toml_config_path()
} else {
config::global_config_path()
};
let raw = file::read_to_string(&path)?;
let mut config: DocumentMut = raw.parse()?;
if let Some(settings) = config["settings"].as_table_like_mut() {
let settings: &mut dyn toml_edit::TableLike =
if let Some((parent_key, child_key)) = key.split_once('.') {
key = child_key;
settings
.entry(parent_key)
.or_insert({
let mut t = toml_edit::Table::new();
t.set_implicit(true);
toml_edit::Item::Table(t)
})
.as_table_like_mut()
.ok_or_else(|| eyre!("Setting [{parent_key}] is not a table"))?
} else {
settings
};
settings.remove(key);
// validate
let _: SettingsFile = toml::from_str(&config.to_string())?;
file::write(&path, config.to_string())?;
}
Ok(())
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise settings unset idiomatic_version_file</bold>
"#
);