diff --git a/e2e/cli/test_settings_unset b/e2e/cli/test_settings_unset new file mode 100644 index 0000000000..3a53c153a5 --- /dev/null +++ b/e2e/cli/test_settings_unset @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +mise settings set python.compile true +assert "mise settings get python.compile" "true" +mise settings unset python.compile +assert_fail "mise settings get python.compile" "mise ERROR Unknown setting: python.compile" diff --git a/src/cli/settings/set.rs b/src/cli/settings/set.rs index d56289c769..2b7ad4b778 100644 --- a/src/cli/settings/set.rs +++ b/src/cli/settings/set.rs @@ -51,32 +51,36 @@ pub fn set(mut key: &str, value: &str, add: bool, local: bool) -> Result<()> { if !config.contains_key("settings") { config["settings"] = toml_edit::Item::Table(toml_edit::Table::new()); } - let mut settings = config["settings"].as_table_mut().unwrap(); - if key.contains(".") { - let (parent_key, child_key) = key.split_once('.').unwrap(); - - key = child_key; - settings = settings - .entry(parent_key) - .or_insert(toml_edit::Item::Table(toml_edit::Table::new())) - .as_table_mut() - .unwrap(); - } - - let value = match settings.get(key).map(|c| c.as_array()) { - Some(Some(array)) if add => { - let mut array = array.clone(); - array.extend(value.as_array().unwrap().iter().cloned()); - array.into() + if let Some(mut settings) = config["settings"].as_table_mut() { + if let Some((parent_key, child_key)) = key.split_once('.') { + key = child_key; + settings = settings + .entry(parent_key) + .or_insert({ + let mut t = toml_edit::Table::new(); + t.set_implicit(true); + toml_edit::Item::Table(t) + }) + .as_table_mut() + .unwrap(); } - _ => value, - }; - settings.insert(key, value.into()); - // validate - let _: SettingsFile = toml::from_str(&config.to_string())?; + let value = match settings.get(key).map(|c| c.as_array()) { + Some(Some(array)) if add => { + let mut array = array.clone(); + array.extend(value.as_array().unwrap().iter().cloned()); + array.into() + } + _ => value, + }; + settings.insert(key, value.into()); + + // validate + let _: SettingsFile = toml::from_str(&config.to_string())?; - file::write(path, config.to_string()) + file::write(path, config.to_string())?; + } + Ok(()) } fn parse_list_by_comma(value: &str) -> Result { diff --git a/src/cli/settings/unset.rs b/src/cli/settings/unset.rs index 1391628cb2..880b03f247 100644 --- a/src/cli/settings/unset.rs +++ b/src/cli/settings/unset.rs @@ -20,24 +20,38 @@ pub struct SettingsUnset { impl SettingsUnset { pub fn run(self) -> Result<()> { - let path = if self.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 !config.contains_key("settings") { - return Ok(()); - } - let settings = config["settings"].as_table_mut().unwrap(); - settings.remove(&self.key); + 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(mut settings) = config["settings"].as_table_mut() { + if let Some((parent_key, child_key)) = key.split_once('.') { + key = child_key; + settings = settings + .entry(parent_key) + .or_insert({ + let mut t = toml_edit::Table::new(); + t.set_implicit(true); + toml_edit::Item::Table(t) + }) + .as_table_mut() + .unwrap(); + } + settings.remove(key); // validate let _: SettingsFile = toml::from_str(&config.to_string())?; - file::write(&path, config.to_string()) + file::write(&path, config.to_string())?; } + Ok(()) } static AFTER_LONG_HELP: &str = color_print::cstr!(