Skip to content

Commit

Permalink
feat: Add setting description to mise settings --json-extended output (
Browse files Browse the repository at this point in the history
…#3919)

* feat: Add setting description to mise settings --json-extended output

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: jdx <216188+jdx@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 5, 2025
1 parent ecfc3f6 commit 56c300e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 4 additions & 0 deletions e2e/cli/test_settings_ls
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ disable_backends = ["rust", "java"]'
assert_contains "mise settings --json-extended" "{
\"all_compile\": {
\"value\": false,
\"type\": \"boolean\",
\"description\": \"do not use precompiled binaries for any tool\",
\"source\": \"$HOME/workdir/mise.toml\"
},
\"disable_backends\": {
\"value\": [
\"rust\",
\"java\"
],
\"type\": \"array\",
\"description\": \"Backends to disable such as \`asdf\` or \`pipx\`\",
\"source\": \"$HOME/workdir/mise.toml\"
}
}"
Expand Down
4 changes: 0 additions & 4 deletions mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ backend = "cargo:cargo-edit"
version = "1.42.0"
backend = "cargo:cargo-insta"

[tools."cargo:cargo-show"]
version = "0.6.0"
backend = "cargo:cargo-show"

[tools."cargo:git-cliff"]
version = "2.7.0"
backend = "cargo:git-cliff"
Expand Down
39 changes: 37 additions & 2 deletions src/cli/settings/ls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config;
use crate::config::settings::{SettingsPartial, SETTINGS_META};
use crate::config::settings::{SettingsPartial, SettingsType, SETTINGS_META};
use crate::config::{Settings, ALL_TOML_CONFIG_FILES, SETTINGS};
use crate::file::display_path;
use crate::ui::table;
Expand Down Expand Up @@ -44,6 +44,19 @@ pub struct SettingsLs {
toml: bool,
}

fn settings_type_to_string(st: &SettingsType) -> String {
match st {
SettingsType::Bool => "boolean".to_string(),
SettingsType::String => "string".to_string(),
SettingsType::Integer => "number".to_string(),
SettingsType::Duration => "number".to_string(),
SettingsType::Path => "string".to_string(),
SettingsType::Url => "string".to_string(),
SettingsType::ListString => "array".to_string(),
SettingsType::ListPath => "array".to_string(),
}
}

impl SettingsLs {
pub fn run(self) -> Result<()> {
if self.complete {
Expand Down Expand Up @@ -129,6 +142,10 @@ impl SettingsLs {
"value".to_string(),
toml_value_to_json_value(row.toml_value),
);
entry.insert("type".to_string(), row.type_.into());
if let Some(description) = row.description {
entry.insert("description".to_string(), description.into());
}
if let Some(source) = row.source {
entry.insert("source".to_string(), source.to_string_lossy().into());
}
Expand Down Expand Up @@ -187,6 +204,10 @@ struct Row {
source: Option<PathBuf>,
#[tabled(skip)]
toml_value: toml::Value,
#[tabled(skip)]
description: Option<String>,
#[tabled(skip)]
type_: String,
}

impl Row {
Expand All @@ -206,21 +227,35 @@ impl Row {
let mut rows = vec![];
if let Some(table) = v.as_table() {
if !table.is_empty() {
rows.reserve(table.len());
let meta = SETTINGS_META.get(k.as_str());
let desc = meta.map(|sm| sm.description.to_string());
let type_str = meta
.map(|sm| settings_type_to_string(&sm.type_))
.unwrap_or_default();

for (subkey, subvalue) in table {
rows.push(Row {
key: format!("{k}.{subkey}"),
value: subvalue.to_string(),
type_: type_str.clone(),
source: source.clone(),
toml_value: subvalue.clone(),
description: desc.clone(),
});
}
}
} else {
let meta = SETTINGS_META.get(k.as_str());
rows.push(Row {
key: k,
key: k.clone(),
value: v.to_string(),
type_: meta
.map(|sm| settings_type_to_string(&sm.type_))
.unwrap_or_default(),
source,
toml_value: v,
description: meta.map(|sm| sm.description.to_string()),
});
}
rows
Expand Down

0 comments on commit 56c300e

Please sign in to comment.