diff --git a/docs/cli/index.md b/docs/cli/index.md index c621f99981..d86f661467 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -136,7 +136,7 @@ Can also use `MISE_NO_CONFIG=1` - [`mise unuse [--no-prune] [--global] ...`](/cli/unuse.md) - [`mise upgrade [FLAGS] [TOOL@VERSION]...`](/cli/upgrade.md) - [`mise use [FLAGS] [TOOL@VERSION]...`](/cli/use.md) -- [`mise version`](/cli/version.md) +- [`mise version [-J --json]`](/cli/version.md) - [`mise watch [FLAGS] [TASK] [ARGS]...`](/cli/watch.md) - [`mise where `](/cli/where.md) - [`mise which [FLAGS] [BIN_NAME]`](/cli/which.md) diff --git a/docs/cli/version.md b/docs/cli/version.md index 132144ffb0..de455aadd7 100644 --- a/docs/cli/version.md +++ b/docs/cli/version.md @@ -1,6 +1,6 @@ # `mise version` -- **Usage**: `mise version` +- **Usage**: `mise version [-J --json]` - **Aliases**: `v` - **Source code**: [`src/cli/version.rs`](https://github.com/jdx/mise/blob/main/src/cli/version.rs) @@ -10,6 +10,12 @@ Displays the version, os, architecture, and the date of the build. If the version is out of date, it will display a warning. +## Flags + +### `-J --json` + +Print the version information in JSON format + Examples: ``` diff --git a/e2e/cli/test_version b/e2e/cli/test_version new file mode 100644 index 0000000000..685ab7b7a5 --- /dev/null +++ b/e2e/cli/test_version @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +assert "mise version --json" diff --git a/mise.usage.kdl b/mise.usage.kdl index eb2d6c5a01..a45a118f61 100644 --- a/mise.usage.kdl +++ b/mise.usage.kdl @@ -954,6 +954,7 @@ cmd version help="Display the version of mise" { alias v long_help "Display the version of mise\n\nDisplays the version, os, architecture, and the date of the build.\n\nIf the version is out of date, it will display a warning." after_long_help "Examples:\n\n $ mise version\n $ mise --version\n $ mise -v\n $ mise -V\n" + flag "-J --json" help="Print the version information in JSON format" } cmd watch help="Run task(s) and watch for changes to rerun it" { alias w diff --git a/src/cli/version.rs b/src/cli/version.rs index 02b9f25257..e0c93381a5 100644 --- a/src/cli/version.rs +++ b/src/cli/version.rs @@ -20,7 +20,36 @@ use crate::{dirs, duration, env, file}; /// If the version is out of date, it will display a warning. #[derive(Debug, clap::Args)] #[clap(verbatim_doc_comment, visible_alias = "v", after_long_help = AFTER_LONG_HELP)] -pub struct Version {} +pub struct Version { + /// Print the version information in JSON format + #[clap(short = 'J', long)] + json: bool, +} + +impl Version { + pub fn run(self) -> Result<()> { + if self.json { + self.json()? + } else { + show_version()?; + show_latest(); + } + Ok(()) + } + + fn json(&self) -> Result<()> { + let json = serde_json::json!({ + "version": *VERSION, + "latest": get_latest_version(duration::DAILY), + "os": *OS, + "arch": *ARCH, + "build_time": BUILD_TIME.to_string(), + "git_sha": git_sha(), + }); + println!("{}", serde_json::to_string_pretty(&json)?); + Ok(()) + } +} pub static OS: Lazy = Lazy::new(|| env::consts::OS.into()); pub static ARCH: Lazy = Lazy::new(|| { @@ -57,14 +86,6 @@ static AFTER_LONG_HELP: &str = color_print::cstr!( pub static V: Lazy = Lazy::new(|| Versioning::new(env!("CARGO_PKG_VERSION")).unwrap()); -impl Version { - pub fn run(self) -> Result<()> { - show_version()?; - show_latest(); - Ok(()) - } -} - pub fn print_version_if_requested(args: &[String]) -> std::io::Result { #[cfg(unix)] let mise_bin = "mise"; diff --git a/xtasks/fig/src/mise.ts b/xtasks/fig/src/mise.ts index 01d06a6763..f1ad71a673 100644 --- a/xtasks/fig/src/mise.ts +++ b/xtasks/fig/src/mise.ts @@ -2749,6 +2749,13 @@ const completionSpec: Fig.Spec = { { name: ["version", "v"], description: "Display the version of mise", + options: [ + { + name: ["-J", "--json"], + description: "Print the version information in JSON format", + isRepeatable: false, + }, + ], }, { name: ["watch", "w"],