Skip to content

Commit

Permalink
Merge pull request #285 from crepererum-oss/crepererum/issue261
Browse files Browse the repository at this point in the history
feat: add `--version` command
  • Loading branch information
crepererum authored Sep 22, 2024
2 parents caef1d4 + 3a5c494 commit e8d5e45
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
27 changes: 27 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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
}
12 changes: 10 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -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"));
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit e8d5e45

Please sign in to comment.