Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
Add --version-json flag (hidden)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Mackie authored and Jordan Mackie committed Mar 1, 2023
1 parent 88da59e commit bc4383f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/ditto-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$ ditto --help
putting the fun in functional

Usage: ditto <COMMAND>
Usage: ditto [COMMAND]

Commands:
bootstrap Bootstrap a new project
Expand Down
15 changes: 13 additions & 2 deletions crates/ditto-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod spinner;
mod version;

use clap::{
arg,
builder::{IntoResettable, Str},
ArgMatches, Command,
};
Expand All @@ -31,8 +32,8 @@ fn command(
.bin_name("ditto")
.version(version_short)
.long_version(version_long)
.arg(arg!(--"version-json").hide(true))
.disable_help_subcommand(true)
.subcommand_required(true)
.about("putting the fun in functional")
.subcommand(bootstrap::command(SUBCOMMAND_BOOTSTRAP).display_order(0))
.subcommand(make::command(SUBCOMMAND_MAKE).display_order(1))
Expand Down Expand Up @@ -65,7 +66,17 @@ async fn run(mut cmd: Command, matches: &ArgMatches, version: &Version) -> Resul
.unwrap();
bootstrap::run(cmd, matches, version)
} else {
unreachable!()
// Print JSON version information if called like
// `ditto --version-json`
if matches.get_flag("version-json") {
println!("{}", serde_json::to_string_pretty(version).unwrap());
return Ok(());
}
// Otherwise print help and exit
cmd.print_help().unwrap();
std::process::exit(1)
// Or could do this...
// clap::Error::new(clap::error::ErrorKind::MissingSubcommand).with_cmd(&cmd).exit();
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ditto-cli/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ static GIT_DIRTY: &str = env!("GIT_DIRTY");
static BUILD_TIME: &str = env!("BUILD_TIME");
static PROFILE: &str = env!("PROFILE");

#[derive(Debug, Clone)]
#[derive(Debug, Clone, serde::Serialize)]
pub struct Version {
pub semversion: semver::Version,
pub git_rev: String,
Expand Down

0 comments on commit bc4383f

Please sign in to comment.