From 98b3951ed7eab4f72b2373666f1652cb79d6ccb4 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:13:31 +0200 Subject: [PATCH] feat: add version --- build.rs | 13 +++++++++++++ src/main.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..89e2a67 --- /dev/null +++ b/build.rs @@ -0,0 +1,13 @@ +fn commit_hash() -> String { + let output = std::process::Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output() + .unwrap(); + String::from_utf8(output.stdout).unwrap() +} + +fn main() { + let hash = commit_hash(); + println!("cargo:rerun-if-env-changed=COMMIT_HASH"); + println!("cargo:rustc-env=COMMIT_HASH={}", hash); +} diff --git a/src/main.rs b/src/main.rs index b5c593f..1360a29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,24 @@ fn main() -> Result<()> { let start_time = Instant::now(); log::init_logger(); + // Version + let args: Vec = std::env::args().collect(); + if args.contains(&"-v".to_string()) || args.contains(&"--version".to_string()) { + let commit_hash = env!("COMMIT_HASH"); + + println!( + "{} {}{}", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + if commit_hash.is_empty() { + "".to_string() + } else { + format!(" ({})", commit_hash).to_string() + } + ); + return Ok(()); + } + let args = Args::parse(); if !args.no_keep_awake {