Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mise settings unset does not seem to work #3867

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/cli/test_settings_unset
Original file line number Diff line number Diff line change
@@ -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"
50 changes: 27 additions & 23 deletions src/cli/settings/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<toml_edit::Value> {
Expand Down
40 changes: 27 additions & 13 deletions src/cli/settings/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
Loading