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

settings: improve set/ls commands #1579

Merged
merged 1 commit into from
Feb 1, 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
3 changes: 1 addition & 2 deletions src/cli/config/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use clap::ValueHint;
use eyre::Result;

use crate::config::{Config, Settings};
use crate::config::Settings;
use crate::file;
use crate::file::display_path;

Expand All @@ -18,7 +18,6 @@ pub struct ConfigGenerate {

impl ConfigGenerate {
pub fn run(self) -> Result<()> {
let _ = Config::try_get()?;
let settings = Settings::try_get()?;
settings.ensure_experimental("`mise config generate`")?;
let doc = r#"
Expand Down
34 changes: 19 additions & 15 deletions src/cli/settings/get.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::collections::BTreeMap;

use serde_json::Value;

use crate::config::{Config, Settings};
use crate::config::Settings;

/// Show a current setting
///
Expand All @@ -19,14 +15,24 @@ pub struct SettingsGet {

impl SettingsGet {
pub fn run(self) -> eyre::Result<()> {
Config::try_get()?;
let settings = Settings::try_get()?;
let json = settings.to_string();
let doc: BTreeMap<String, Value> = serde_json::from_str(&json)?;
match doc.get(&self.setting) {
Some(value) => Ok(miseprintln!("{}", value)),
None => Err(eyre!("Unknown setting: {}", self.setting)),
let mut value = toml::Value::Table(settings.as_dict()?);
let mut key = Some(self.setting.as_str());
while let Some(k) = key {
let k = k
.split_once('.')
.map(|(a, b)| (a, Some(b)))
.unwrap_or((k, None));
if let Some(v) = value.as_table().and_then(|t| t.get(k.0)) {
key = k.1;
value = v.clone()
} else {
bail!("Unknown setting: {}", self.setting);
}
}
miseprintln!("{}", value);

Ok(())
}
}

Expand All @@ -44,10 +50,8 @@ mod tests {
#[test]
fn test_settings_get() {
reset_config();
let stdout = assert_cli!("settings", "get", "legacy_version_file");
assert_snapshot!(stdout, @r###"
true
"###);
assert_cli_snapshot!("settings", "get", "legacy_version_file", @"true");
assert_cli_snapshot!("settings", "get", "status.missing_tools", @r###""if_other_versions_installed""###);
}

#[test]
Expand Down
29 changes: 9 additions & 20 deletions src/cli/settings/ls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::collections::BTreeMap;

use eyre::Result;
use serde_json::Value;

use crate::config::Settings;

Expand All @@ -18,18 +15,11 @@ pub struct SettingsLs {}
impl SettingsLs {
pub fn run(self) -> Result<()> {
let settings = Settings::try_get()?;
let json = settings.to_string();
let doc: BTreeMap<String, Value> = serde_json::from_str(&json)?;
for (key, value) in doc {
if Settings::hidden_configs().contains(key.as_str()) {
continue;
}
if key == "status" {
// TODO: print this properly
continue;
}
miseprintln!("{} = {}", key, value);
let mut settings = settings.as_dict()?;
for k in Settings::hidden_configs() {
settings.remove(*k);
}
miseprintln!("{}", settings);
Ok(())
}
}
Expand Down Expand Up @@ -68,19 +58,18 @@ mod tests {
plugin_autoupdate_last_check_duration = "20m"
python_compile = false
python_default_packages_file = "~/.default-python-packages"
python_patch_url = null
python_patches_directory = null
python_precompiled_arch = null
python_precompiled_os = null
python_pyenv_repo = "https://github.com/pyenv/pyenv.git"
python_venv_auto_create = false
quiet = false
raw = false
shorthands_file = null
task_output = null
trusted_config_paths = []
verbose = true
yes = true

[status]
missing_tools = "if_other_versions_installed"
show_env = false
show_tools = false
"###);
}
}
36 changes: 18 additions & 18 deletions src/cli/settings/set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use eyre::Result;
use toml_edit::Document;

use crate::config::settings::SettingsStatusMissingTools;
use crate::config::settings::SettingsFile;

use crate::{env, file};

/// Add/update a setting
Expand Down Expand Up @@ -34,17 +35,13 @@ impl SettingsSet {
"node_compile" => parse_bool(&self.value)?,
"not_found_auto_install" => parse_bool(&self.value)?,
"paranoid" => parse_bool(&self.value)?,
"plugin_autoupdate_last_check_duration" => parse_i64(&self.value)?,
"plugin_autoupdate_last_check_duration" => self.value.into(),
"python_compile" => parse_bool(&self.value)?,
"python_venv_auto_create" => parse_bool(&self.value)?,
"quiet" => parse_bool(&self.value)?,
"raw" => parse_bool(&self.value)?,
"shorthands_file" => self.value.into(),
"status.missing_tools" => self
.value
.parse::<SettingsStatusMissingTools>()?
.to_string()
.into(),
"status.missing_tools" => self.value.into(),
"status.show_env" => parse_bool(&self.value)?,
"status.show_tools" => parse_bool(&self.value)?,
"task_output" => self.value.into(),
Expand Down Expand Up @@ -72,6 +69,10 @@ impl SettingsSet {
} else {
settings.insert(&self.setting, toml_edit::Item::Value(value));
}

// validate
let _: SettingsFile = toml::from_str(&config.to_string())?;

file::write(path, config.to_string())
}
}
Expand Down Expand Up @@ -117,36 +118,35 @@ pub mod tests {
assert_cli_snapshot!("settings", @r###"
activate_aggressive = false
all_compile = false
always_keep_download = false
always_keep_install = false
always_keep_download = true
always_keep_install = true
asdf_compat = false
cargo_binstall = true
color = true
disable_default_shorthands = false
disable_tools = []
experimental = true
jobs = 4
legacy_version_file = true
jobs = 2
legacy_version_file = false
legacy_version_file_disable_tools = []
node_compile = false
not_found_auto_install = true
paranoid = false
plugin_autoupdate_last_check_duration = "7d"
plugin_autoupdate_last_check_duration = "1"
python_compile = false
python_default_packages_file = "~/.default-python-packages"
python_patch_url = null
python_patches_directory = null
python_precompiled_arch = null
python_precompiled_os = null
python_pyenv_repo = "https://github.com/pyenv/pyenv.git"
python_venv_auto_create = false
quiet = false
raw = false
shorthands_file = null
task_output = null
trusted_config_paths = []
verbose = true
yes = true

[status]
missing_tools = "never"
show_env = false
show_tools = false
"###);
reset_config();
}
Expand Down
11 changes: 5 additions & 6 deletions src/cli/settings/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,18 @@ mod tests {
plugin_autoupdate_last_check_duration = "20m"
python_compile = false
python_default_packages_file = "~/.default-python-packages"
python_patch_url = null
python_patches_directory = null
python_precompiled_arch = null
python_precompiled_os = null
python_pyenv_repo = "https://github.com/pyenv/pyenv.git"
python_venv_auto_create = false
quiet = false
raw = false
shorthands_file = null
task_output = null
trusted_config_paths = []
verbose = true
yes = true

[status]
missing_tools = "if_other_versions_installed"
show_env = false
show_tools = false
"###);

reset_config();
Expand Down
8 changes: 6 additions & 2 deletions src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,17 @@ impl Settings {
}
})
}

pub fn as_dict(&self) -> eyre::Result<toml::Table> {
Ok(self.to_string().parse()?)
}
}

impl Display for Settings {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match serde_json::to_string_pretty(self) {
match toml::to_string_pretty(self) {
Ok(s) => write!(f, "{}", s),
Err(e) => std::fmt::Result::Err(std::fmt::Error::custom(e)),
Err(e) => Err(std::fmt::Error::custom(e)),
}
}
}
Loading