From ac22b1bed13c298b566acf503cba47e7e9bc072a Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Mon, 11 Mar 2024 20:11:59 -0700 Subject: [PATCH] allow including extra info in `--version` output --- Cargo.toml | 2 +- flake.nix | 1 + src/clap.rs | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08188cb3..796791bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,7 @@ thread_local = "1.1.7" hmac = "0.12.1" sha-1 = "0.10.1" # used for conduit's CLI and admin room command parsing -clap = { version = "4.3.0", default-features = false, features = ["std", "derive", "help", "usage", "error-context"] } +clap = { version = "4.3.0", default-features = false, features = ["std", "derive", "help", "usage", "error-context", "string"] } futures-util = { version = "0.3.28", default-features = false } # Used for reading the configuration from conduit.toml & environment variables figment = { version = "0.10.8", features = ["env", "toml"] } diff --git a/flake.nix b/flake.nix index cdfe91f8..97f68354 100644 --- a/flake.nix +++ b/flake.nix @@ -73,6 +73,7 @@ }); env = pkgs: { + CONDUIT_VERSION_EXTRA = self.shortRev or self.dirtyShortRev; ROCKSDB_INCLUDE_DIR = "${rocksdb' pkgs}/include"; ROCKSDB_LIB_DIR = "${rocksdb' pkgs}/lib"; } diff --git a/src/clap.rs b/src/clap.rs index 444f21bf..170d2a17 100644 --- a/src/clap.rs +++ b/src/clap.rs @@ -2,9 +2,23 @@ use clap::Parser; +/// Returns the current version of the crate with extra info if supplied +/// +/// Set the environment variable `CONDUIT_VERSION_EXTRA` to any UTF-8 string to +/// include it in parenthesis after the SemVer version. A common value are git +/// commit hashes. +fn version() -> String { + let cargo_pkg_version = env!("CARGO_PKG_VERSION"); + + match option_env!("CONDUIT_VERSION_EXTRA") { + Some(x) => format!("{} ({})", cargo_pkg_version, x), + None => cargo_pkg_version.to_owned(), + } +} + /// Command line arguments #[derive(Parser)] -#[clap(about, version)] +#[clap(about, version = version())] pub struct Args {} /// Parse command line arguments into structured data