From 2cd7e94de3c67e1b1e0e6eb6416df3eb4ad99924 Mon Sep 17 00:00:00 2001 From: hverlin Date: Mon, 2 Dec 2024 23:26:48 +0100 Subject: [PATCH] feat: Add `json-with-sources` option to settings ls (#3307) --- docs/cli/settings.md | 4 ++++ docs/cli/settings/ls.md | 4 ++++ e2e/cli/test_settings_ls | 31 ++++++++++++++++++++++++++++- mise.usage.kdl | 2 ++ src/cli/settings/ls.rs | 43 ++++++++++++++++++++++++++++++++++++++-- src/cli/settings/mod.rs | 9 +++++++-- xtasks/fig/src/mise.ts | 14 +++++++++++++ 7 files changed, 102 insertions(+), 5 deletions(-) diff --git a/docs/cli/settings.md b/docs/cli/settings.md index 5ffce42d32..4a4e24404a 100644 --- a/docs/cli/settings.md +++ b/docs/cli/settings.md @@ -31,6 +31,10 @@ List all settings Output in JSON format +### `--json-extended` + +Output in JSON format with sources + ### `-T --toml` Output in TOML format diff --git a/docs/cli/settings/ls.md b/docs/cli/settings/ls.md index 2267391106..42f4c43b80 100644 --- a/docs/cli/settings/ls.md +++ b/docs/cli/settings/ls.md @@ -31,6 +31,10 @@ Use the local config file instead of the global one Output in JSON format +### `--json-extended` + +Output in JSON format with sources + ### `-T --toml` Output in TOML format diff --git a/e2e/cli/test_settings_ls b/e2e/cli/test_settings_ls index fb28597e7e..f328893139 100644 --- a/e2e/cli/test_settings_ls +++ b/e2e/cli/test_settings_ls @@ -1,7 +1,36 @@ #!/usr/bin/env bash echo "settings.all_compile = false" >mise.toml -assert_contains "mise settings" "all_compile false" +echo "settings.disable_backends = [\"rust\", \"java\"]" >>mise.toml + +assert_contains "mise settings" 'all_compile false ~/workdir/mise.toml +disable_backends ["rust", "java"] ~/workdir/mise.toml' + +assert_contains "mise settings --json" '{ + "all_compile": false, + "disable_backends": [ + "rust", + "java" + ] +}' + +assert_contains "mise settings --toml" 'all_compile = false +disable_backends = ["rust", "java"]' + +assert_contains "mise settings --json-extended" '{ + "all_compile": { + "source": "~/workdir/mise.toml", + "value": false + }, + "disable_backends": { + "source": "~/workdir/mise.toml", + "value": [ + "rust", + "java" + ] + } +}' + assert_contains "mise settings ls -T" "all_compile = false" echo "settings.python.venv_auto_create = false" >>mise.toml assert_contains "mise settings ls python" "venv_auto_create false" diff --git a/mise.usage.kdl b/mise.usage.kdl index 19b72cc110..98643edddb 100644 --- a/mise.usage.kdl +++ b/mise.usage.kdl @@ -1062,6 +1062,7 @@ cmd "settings" help="Manage settings" { flag "-a --all" help="List all settings" flag "-l --local" help="Use the local config file instead of the global one" global=true flag "-J --json" help="Output in JSON format" + flag "--json-extended" help="Output in JSON format with sources" flag "-T --toml" help="Output in TOML format" arg "[KEY]" help="Setting name to get/set" arg "[VALUE]" help="Setting value to set" @@ -1114,6 +1115,7 @@ but managed separately with `mise aliases`" flag "-a --all" help="Display settings set to the default" flag "-l --local" help="Use the local config file instead of the global one" flag "-J --json" help="Output in JSON format" + flag "--json-extended" help="Output in JSON format with sources" flag "-T --toml" help="Output in TOML format" arg "[KEY]" help="List keys under this key" } diff --git a/src/cli/settings/ls.rs b/src/cli/settings/ls.rs index ca615c3e16..9ab827698e 100644 --- a/src/cli/settings/ls.rs +++ b/src/cli/settings/ls.rs @@ -28,11 +28,15 @@ pub struct SettingsLs { pub local: bool, /// Output in JSON format - #[clap(long, short = 'J')] + #[clap(long, short = 'J', group = "output")] pub json: bool, + /// Output in JSON format with sources + #[clap(long, group = "output")] + pub json_extended: bool, + /// Output in TOML format - #[clap(long, short = 'T')] + #[clap(long, short = 'T', group = "output")] pub toml: bool, } @@ -74,6 +78,8 @@ impl SettingsLs { } if self.json { self.print_json(rows)?; + } else if self.json_extended { + self.print_json_extended(rows)?; } else if self.toml { self.print_toml(rows)?; } else { @@ -101,6 +107,31 @@ impl SettingsLs { Ok(()) } + fn print_json_extended(&self, rows: Vec) -> Result<()> { + let mut table = serde_json::Map::new(); + for row in rows { + let mut entry = serde_json::Map::new(); + entry.insert( + "value".to_string(), + toml_value_to_json_value(row.toml_value), + ); + if let Some(source) = row.source { + entry.insert("source".to_string(), display_path(&source).into()); + } + if let Some((key, subkey)) = row.key.split_once('.') { + let subtable = table + .entry(key) + .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new())); + let subtable = subtable.as_object_mut().unwrap(); + subtable.insert(subkey.to_string(), entry.into()); + } else { + table.insert(row.key, entry.into()); + } + } + miseprintln!("{}", serde_json::to_string_pretty(&table)?); + Ok(()) + } + fn print_toml(&self, rows: Vec) -> Result<()> { let mut table = toml::Table::new(); for row in rows { @@ -188,6 +219,14 @@ fn toml_value_to_json_value(v: toml::Value) -> serde_json::Value { toml::Value::Integer(i) => i.into(), toml::Value::Boolean(b) => b.into(), toml::Value::Float(f) => f.into(), + toml::Value::Table(t) => { + let mut table = serde_json::Map::new(); + for (k, v) in t { + table.insert(k, toml_value_to_json_value(v)); + } + table.into() + } + toml::Value::Array(a) => a.into_iter().map(toml_value_to_json_value).collect(), v => v.to_string().into(), } } diff --git a/src/cli/settings/mod.rs b/src/cli/settings/mod.rs index 1942964ff8..7a3b69e5c1 100644 --- a/src/cli/settings/mod.rs +++ b/src/cli/settings/mod.rs @@ -30,11 +30,15 @@ pub struct Settings { local: bool, /// Output in JSON format - #[clap(long, short = 'J')] + #[clap(long, short = 'J', group = "output")] pub json: bool, + /// Output in JSON format with sources + #[clap(long, group = "output")] + pub json_extended: bool, + /// Output in TOML format - #[clap(long, short = 'T')] + #[clap(long, short = 'T', group = "output")] pub toml: bool, } @@ -87,6 +91,7 @@ impl Settings { key: None, local: self.local, json: self.json, + json_extended: self.json_extended, toml: self.toml, }) } diff --git a/xtasks/fig/src/mise.ts b/xtasks/fig/src/mise.ts index 11b7cb4a3d..37b7bbdf7c 100644 --- a/xtasks/fig/src/mise.ts +++ b/xtasks/fig/src/mise.ts @@ -2039,6 +2039,13 @@ const completionSpec: Fig.Spec = { "description": "Output in JSON format", "isRepeatable": false }, + { + "name": [ + "--json-extended" + ], + "description": "Output in JSON format with sources", + "isRepeatable": false + }, { "name": [ "-T", @@ -2142,6 +2149,13 @@ const completionSpec: Fig.Spec = { "description": "Output in JSON format", "isRepeatable": false }, + { + "name": [ + "--json-extended" + ], + "description": "Output in JSON format with sources", + "isRepeatable": false + }, { "name": [ "-T",