From 3a5c4945e97d44c3f009ccb308a9e5ad7b1c75fc Mon Sep 17 00:00:00 2001 From: Marco Neumann Date: Sun, 22 Sep 2024 18:07:34 +0200 Subject: [PATCH] feat: add `--version` command Closes #261. --- build.rs | 27 +++++++++++++++++++++++++++ src/constants.rs | 12 ++++++++++-- src/main.rs | 5 +++++ tests/cli.rs | 6 ++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..f7f08fd --- /dev/null +++ b/build.rs @@ -0,0 +1,27 @@ +// Include the GIT_HASH, in `GIT_HASH` environment variable at build +// time, panic'ing if it can not be found +// +// https://stackoverflow.com/questions/43753491/include-git-commit-hash-as-string-into-rust-program +use std::process::Command; + +fn main() -> Result<(), Box> { + println!("cargo:rerun-if-env-changed=GIT_HASH"); + // Populate env!(GIT_HASH) with the current git commit + println!("cargo:rustc-env=GIT_HASH={}", get_git_hash()); + + Ok(()) +} + +fn get_git_hash() -> String { + let git_hash = { + let output = Command::new("git") + .args(["describe", "--always", "--dirty", "--abbrev=64"]) + .output() + .expect("failed to execute git rev-parse to read the current git hash"); + + String::from_utf8(output.stdout).expect("non-utf8 found in git hash") + }; + + assert!(!git_hash.is_empty(), "attempting to embed empty git hash"); + git_hash +} diff --git a/src/constants.rs b/src/constants.rs index bb02bbf..3fb4b92 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,2 +1,10 @@ -pub(crate) static APP_USER_AGENT: &str = - concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); +pub(crate) static APP_USER_AGENT: &str = concat!( + env!("CARGO_PKG_NAME"), + "/", + env!("CARGO_PKG_VERSION"), + ", revision ", + env!("GIT_HASH") +); + +pub(crate) static VERSION_STRING: &str = + concat!(env!("CARGO_PKG_VERSION"), ", revision ", env!("GIT_HASH")); diff --git a/src/main.rs b/src/main.rs index 3714308..4bf4a98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use crate::{ }; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; +use constants::VERSION_STRING; use folders::Folder; use futures::{StreamExt, TryStreamExt}; use logging::{setup_logging, LoggingCLIConfig}; @@ -40,6 +41,10 @@ mod signal; /// CLI args. #[derive(Debug, Parser)] +#[command( + about = "CLI (Command Line Interface) for Tutanota/Tuta, mostly meant for mass export.", + version = VERSION_STRING, +)] struct Args { /// Logging config. #[clap(flatten)] diff --git a/tests/cli.rs b/tests/cli.rs index 6634dcb..c85b2f0 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -20,6 +20,12 @@ fn test_help_cmd() { cmd.arg("help").assert().success(); } +#[test] +fn test_version_arg() { + let mut cmd = cmd(); + cmd.arg("--version").assert().success(); +} + fn cmd() -> Command { Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap() }