Skip to content

Commit

Permalink
Add libnitrokey version to -V/--version output
Browse files Browse the repository at this point in the history
Currently, it is hard to determine the libnitrokey version used by
nitrocli unless it is linked against a shard library. This patch adds
the libnitrokey version (nitrokey::get_library_version()) to the
-V/--version output.

Fixes #147
  • Loading branch information
robinkrahl authored and d-e-s-o committed Apr 14, 2021
1 parent 537d29a commit 3d4ec0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ffi;

/// Provides access to a Nitrokey device
#[derive(Debug, structopt::StructOpt)]
#[structopt(name = "nitrocli")]
#[structopt(name = "nitrocli", no_version)]
pub struct Args {
/// Increases the log level (can be supplied multiple times)
#[structopt(short, long, global = true, parse(from_occurrences))]
Expand Down
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ impl error::Error for DirectExitError {}

/// Parse the command-line arguments and execute the selected command.
fn handle_arguments(ctx: &mut Context<'_>, argv: Vec<String>) -> anyhow::Result<()> {
match args::Args::from_iter_safe(argv.iter()) {
Ok(args) => {
let version = get_version_string();
let clap = args::Args::clap().version(version.as_str());
match clap.get_matches_from_safe(argv.iter()) {
Ok(matches) => {
let args = args::Args::from_clap(&matches);
ctx.config.update(&args);
args.cmd.execute(ctx)
}
Expand Down Expand Up @@ -157,6 +160,15 @@ fn handle_arguments(ctx: &mut Context<'_>, argv: Vec<String>) -> anyhow::Result<
}
}

fn get_version_string() -> String {
let version = env!("CARGO_PKG_VERSION");
if let Ok(library_version) = nitrokey::get_library_version() {
format!("{} using libnitrokey {}", version, library_version)
} else {
format!("{} using an undetectable libnitrokey version", version)
}
}

/// The context used when running the program.
#[allow(missing_debug_implementations)]
pub struct Context<'io> {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn version_option() {
assert!(re.is_match(&s), out);
}

let re = regex::Regex::new(r"^nitrocli \d+.\d+.\d+(-[^-]+)*\n$").unwrap();
let re = regex::Regex::new(r"^nitrocli \d+.\d+.\d+(-[^-]+)* using libnitrokey .*\n$").unwrap();

test(&re, "--version");
test(&re, "-V");
Expand Down

0 comments on commit 3d4ec0a

Please sign in to comment.