Skip to content

Commit

Permalink
feat: Add json-with-sources option to settings ls
Browse files Browse the repository at this point in the history
  • Loading branch information
hverlin committed Dec 2, 2024
1 parent 503d758 commit fbfeffd
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/cli/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/cli/settings/ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion e2e/cli/test_settings_ls
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
Expand Down
43 changes: 41 additions & 2 deletions src/cli/settings/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -101,6 +107,31 @@ impl SettingsLs {
Ok(())
}

fn print_json_extended(&self, rows: Vec<Row>) -> 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<Row>) -> Result<()> {
let mut table = toml::Table::new();
for row in rows {
Expand Down Expand Up @@ -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(),
}
}
9 changes: 7 additions & 2 deletions src/cli/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -87,6 +91,7 @@ impl Settings {
key: None,
local: self.local,
json: self.json,
json_extended: self.json_extended,
toml: self.toml,
})
}
Expand Down
14 changes: 14 additions & 0 deletions xtasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fbfeffd

Please sign in to comment.