From 55bd3925d9d8c9b6b85cf04c4089139341950b86 Mon Sep 17 00:00:00 2001 From: KIMJiho <52690007+CHOUMnote@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:09:24 +0900 Subject: [PATCH] feat: Define custom `raftify-cli` --version command (#161) * add long_version opt * feat: Print also raftify features --------- Co-authored-by: Gyubong --- Cargo.lock | 24 +++++++++++++++++++++++- raftify-cli/Cargo.lock | 24 +++++++++++++++++++++++- raftify-cli/Cargo.toml | 5 ++++- raftify-cli/build.rs | 16 ++++++++++++++++ raftify-cli/src/mod.rs | 12 ++++++++++-- 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c1a099d..6c857f92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -773,6 +773,26 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "const_format" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbc3a6725e090f416f3c12d12605d90d6be4fde8bd762d236535ab02b388c70d" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e029ee8893eaad89abcb0885453d9555b8cb7982ac456ad044884725215bce42" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -2311,16 +2331,18 @@ dependencies = [ [[package]] name = "raftify_cli" -version = "0.1.81" +version = "0.1.82" dependencies = [ "built", "clap 4.5.19", "comfy-table", + "const_format", "log", "raftify", "serde", "serde_json", "slog", + "toml 0.8.19", "tonic-build", ] diff --git a/raftify-cli/Cargo.lock b/raftify-cli/Cargo.lock index e188616b..ae8d906c 100644 --- a/raftify-cli/Cargo.lock +++ b/raftify-cli/Cargo.lock @@ -459,6 +459,26 @@ dependencies = [ "tiny-keccak", ] +[[package]] +name = "const_format" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbc3a6725e090f416f3c12d12605d90d6be4fde8bd762d236535ab02b388c70d" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e029ee8893eaad89abcb0885453d9555b8cb7982ac456ad044884725215bce42" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "convert_case" version = "0.6.0" @@ -1629,16 +1649,18 @@ dependencies = [ [[package]] name = "raftify_cli" -version = "0.1.81" +version = "0.1.82" dependencies = [ "built", "clap", "comfy-table", + "const_format", "log", "raftify", "serde", "serde_json", "slog", + "toml 0.8.19", "tonic-build", ] diff --git a/raftify-cli/Cargo.toml b/raftify-cli/Cargo.toml index a7423ed8..0bded573 100644 --- a/raftify-cli/Cargo.toml +++ b/raftify-cli/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "raftify_cli" -version = "0.1.81" +version = "0.1.82" edition = "2021" description = "Raftify CLI tool" license = "MIT/Apache-2.0" +build = "build.rs" [dependencies] log = { version = "0.4", features = ["std"] } @@ -14,6 +15,7 @@ built = "0.5" clap = { version = "4.5.18", features = ["derive"] } raftify = { version = "=0.1.81", features = ["heed_storage", "inmemory_storage", "rocksdb_storage"] } comfy-table = "7.1.1" +cfmt = { version = "0.1.0", package = "const_format" } [lib] name = "raftify_cli" @@ -22,3 +24,4 @@ path = "src/mod.rs" [build-dependencies] tonic-build = "0.9.2" built = "0.5" +toml = "0.8.19" diff --git a/raftify-cli/build.rs b/raftify-cli/build.rs index a5c32511..c449e4b2 100644 --- a/raftify-cli/build.rs +++ b/raftify-cli/build.rs @@ -1,4 +1,20 @@ +use std::fs; +use std::path::Path; +use toml::Value; + fn main() -> Result<(), Box> { + let path = Path::new("Cargo.toml"); + let read = fs::read_to_string(path) + .expect("Failed to read Cargo.toml"); + let toml: Value = read.parse::().expect("Failed to parse Cargo.toml"); + + if let Some(raftify_dep) = toml.get("dependencies").and_then(|deps| deps.get("raftify")) { + let version = raftify_dep.get("version").unwrap().as_str().unwrap(); + let features = raftify_dep.get("features").unwrap().as_array().unwrap().iter().map(|f| f.as_str().unwrap()).collect::>().join(", "); + println!("cargo:rustc-env=RAFTIFY_VERSION={}", &version[1..]); + println!("cargo:rustc-env=RAFTIFY_FEATURES={}", features); + } + built::write_built_file().expect("Failed to acquire build-time information"); Ok(()) } diff --git a/raftify-cli/src/mod.rs b/raftify-cli/src/mod.rs index 9666607a..90654324 100644 --- a/raftify-cli/src/mod.rs +++ b/raftify-cli/src/mod.rs @@ -11,11 +11,19 @@ use raftify::{ AbstractLogEntry, AbstractStateMachine, CustomFormatter, Result, StableStorage, }; +use cfmt::formatcp; + +const RAFTIFY_VERSION: &str = env!("RAFTIFY_VERSION"); +const RAFTIFY_FEATURES: &str = env!("RAFTIFY_FEATURES"); +const VERSION_TEXT: &'static str = formatcp!("{PKG_VERSION} +(Built with raftify {}, Enabled features: {})", RAFTIFY_VERSION, RAFTIFY_FEATURES); + #[derive(Parser)] -#[command(name = "raftify")] -#[command(version = PKG_VERSION)] +#[command(name = PKG_NAME)] #[command(author = PKG_AUTHORS)] #[command(about = PKG_DESCRIPTION)] +#[command(version = VERSION_TEXT)] +#[command(long_version = VERSION_TEXT)] struct App { #[command(subcommand)] command: Commands,