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

feat: added mise version --json flag #4061

Merged
merged 2 commits into from
Jan 11, 2025
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
2 changes: 1 addition & 1 deletion docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Can also use `MISE_NO_CONFIG=1`
- [`mise unuse [--no-prune] [--global] <INSTALLED_TOOL@VERSION>...`](/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 <TOOL@VERSION>`](/cli/where.md)
- [`mise which [FLAGS] [BIN_NAME]`](/cli/which.md)
8 changes: 7 additions & 1 deletion docs/cli/version.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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:

```
Expand Down
3 changes: 3 additions & 0 deletions e2e/cli/test_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

assert "mise version --json"
1 change: 1 addition & 0 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 30 additions & 9 deletions src/cli/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Lazy::new(|| env::consts::OS.into());
pub static ARCH: Lazy<String> = Lazy::new(|| {
Expand Down Expand Up @@ -57,14 +86,6 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(

pub static V: Lazy<Versioning> = 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<bool> {
#[cfg(unix)]
let mise_bin = "mise";
Expand Down
7 changes: 7 additions & 0 deletions xtasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Loading