From f5b1cd5525c4b74ad3fbbd571fd138a33e271777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Thu, 24 Mar 2022 16:17:17 +0100 Subject: [PATCH] Add command-line option for printing version and commit info --- Cargo.lock | 30 +++++++++++++++++++++++ aquatic/src/main.rs | 3 +++ aquatic_cli_helpers/Cargo.toml | 1 + aquatic_cli_helpers/src/lib.rs | 38 +++++++++++++++++++++++++++--- aquatic_http/src/lib.rs | 1 + aquatic_http/src/main.rs | 2 +- aquatic_http_load_test/src/main.rs | 1 + aquatic_udp/src/lib.rs | 1 + aquatic_udp/src/main.rs | 1 + aquatic_udp_bench/src/main.rs | 1 + aquatic_udp_load_test/src/main.rs | 1 + aquatic_ws/src/lib.rs | 1 + aquatic_ws/src/main.rs | 2 +- aquatic_ws_load_test/src/main.rs | 1 + 14 files changed, 79 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a07380d..f5e04dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,7 @@ version = "0.1.0" dependencies = [ "anyhow", "aquatic_toml_config", + "git-testament", "log", "serde", "simple_logger", @@ -962,6 +963,29 @@ version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" +[[package]] +name = "git-testament" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "080c47ef3c243fb13474429c14dce386021cd64de731c353998a745c2fa2435b" +dependencies = [ + "git-testament-derive", + "no-std-compat", +] + +[[package]] +name = "git-testament-derive" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0803898541a48d6f0809fa681bc8d38603f727d191f179631d85ddc3b6a9a2c" +dependencies = [ + "log", + "proc-macro2", + "quote", + "syn", + "time", +] + [[package]] name = "glommio" version = "0.7.0" @@ -1338,6 +1362,12 @@ dependencies = [ "memoffset 0.6.5", ] +[[package]] +name = "no-std-compat" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" + [[package]] name = "nodrop" version = "0.1.14" diff --git a/aquatic/src/main.rs b/aquatic/src/main.rs index 5ec47bd..c320e41 100644 --- a/aquatic/src/main.rs +++ b/aquatic/src/main.rs @@ -43,16 +43,19 @@ fn run() -> Result<(), Option> { match protocol.as_str() { "udp" => run_app_with_cli_and_config::( aquatic_udp::APP_NAME, + aquatic_udp::APP_VERSION, aquatic_udp::run, Some(options), ), "http" => run_app_with_cli_and_config::( aquatic_http::APP_NAME, + aquatic_http::APP_VERSION, aquatic_http::run, Some(options), ), "ws" => run_app_with_cli_and_config::( aquatic_ws::APP_NAME, + aquatic_ws::APP_VERSION, aquatic_ws::run, Some(options), ), diff --git a/aquatic_cli_helpers/Cargo.toml b/aquatic_cli_helpers/Cargo.toml index 8094be5..df80062 100644 --- a/aquatic_cli_helpers/Cargo.toml +++ b/aquatic_cli_helpers/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://github.com/greatest-ape/aquatic" aquatic_toml_config = "0.1.0" anyhow = "1" +git-testament = "0.2" log = "0.4" serde = { version = "1", features = ["derive"] } simple_logger = { version = "2", features = ["stderr"] } diff --git a/aquatic_cli_helpers/src/lib.rs b/aquatic_cli_helpers/src/lib.rs index 39b7585..8455856 100644 --- a/aquatic_cli_helpers/src/lib.rs +++ b/aquatic_cli_helpers/src/lib.rs @@ -3,6 +3,7 @@ use std::io::Read; use anyhow::Context; use aquatic_toml_config::TomlConfig; +use git_testament::{git_testament, CommitKind}; use log::LevelFilter; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use simple_logger::SimpleLogger; @@ -35,6 +36,7 @@ pub trait Config: Default + TomlConfig + DeserializeOwned { pub struct Options { config_file: Option, print_config: bool, + print_version: bool, } impl Options { @@ -57,6 +59,9 @@ impl Options { "-p" | "--print-config" => { options.print_config = true; } + "-v" | "--version" => { + options.print_version = true; + } "-h" | "--help" => { return Err(None); } @@ -75,13 +80,14 @@ impl Options { pub fn run_app_with_cli_and_config( app_title: &str, + crate_version: &str, // Function that takes config file and runs application app_fn: fn(T) -> anyhow::Result<()>, opts: Option, ) where T: Config, { - ::std::process::exit(match run_inner(app_title, app_fn, opts) { + ::std::process::exit(match run_inner(app_title, crate_version, app_fn, opts) { Ok(()) => 0, Err(err) => { eprintln!("Error: {:#}", err); @@ -93,6 +99,7 @@ pub fn run_app_with_cli_and_config( fn run_inner( app_title: &str, + crate_version: &str, // Function that takes config file and runs application app_fn: fn(T) -> anyhow::Result<()>, // Possibly preparsed options @@ -120,7 +127,13 @@ where } }; - if options.print_config { + if options.print_version { + let commit_info = get_commit_info(); + + println!("{}{}", crate_version, commit_info); + + Ok(()) + } else if options.print_config { print!("{}", default_config_as_toml::()); Ok(()) @@ -147,8 +160,9 @@ where println!("\nOptions:"); println!(" -c, --config-file Load config from this path"); - println!(" -p, --print-config Print default config"); println!(" -h, --help Print this help message"); + println!(" -p, --print-config Print default config"); + println!(" -v, --version Print version information"); if let Some(error) = opt_error { println!("\nError: {}.", error); @@ -195,3 +209,21 @@ fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> { Ok(()) } + +fn get_commit_info() -> String { + git_testament!(TESTAMENT); + + match TESTAMENT.commit { + CommitKind::NoTags(hash, date) => { + format!(" ({} - {})", first_8_chars(hash), date) + } + CommitKind::FromTag(_tag, hash, date, _tag_distance) => { + format!(" ({} - {})", first_8_chars(hash), date) + } + _ => String::new(), + } +} + +fn first_8_chars(input: &str) -> String { + input.chars().take(8).collect() +} diff --git a/aquatic_http/src/lib.rs b/aquatic_http/src/lib.rs index f6ce2b0..b484a53 100644 --- a/aquatic_http/src/lib.rs +++ b/aquatic_http/src/lib.rs @@ -19,6 +19,7 @@ pub mod config; mod workers; pub const APP_NAME: &str = "aquatic_http: BitTorrent tracker (HTTP over TLS)"; +pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); const SHARED_CHANNEL_SIZE: usize = 1024; diff --git a/aquatic_http/src/main.rs b/aquatic_http/src/main.rs index 221122f..6116626 100644 --- a/aquatic_http/src/main.rs +++ b/aquatic_http/src/main.rs @@ -5,5 +5,5 @@ use aquatic_http::config::Config; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; fn main() { - run_app_with_cli_and_config::(aquatic_http::APP_NAME, aquatic_http::run, None) + run_app_with_cli_and_config::(aquatic_http::APP_NAME, aquatic_http::APP_VERSION, aquatic_http::run, None) } diff --git a/aquatic_http_load_test/src/main.rs b/aquatic_http_load_test/src/main.rs index f5ea255..3cdef32 100644 --- a/aquatic_http_load_test/src/main.rs +++ b/aquatic_http_load_test/src/main.rs @@ -26,6 +26,7 @@ const MBITS_FACTOR: f64 = 1.0 / ((1024.0 * 1024.0) / 8.0); pub fn main() { aquatic_cli_helpers::run_app_with_cli_and_config::( "aquatic_http_load_test: BitTorrent load tester", + env!("CARGO_PKG_VERSION"), run, None, ) diff --git a/aquatic_udp/src/lib.rs b/aquatic_udp/src/lib.rs index 3f19a25..7814f6f 100644 --- a/aquatic_udp/src/lib.rs +++ b/aquatic_udp/src/lib.rs @@ -23,6 +23,7 @@ use common::{ConnectedRequestSender, ConnectedResponseSender, SocketWorkerIndex, use crate::common::RequestWorkerIndex; pub const APP_NAME: &str = "aquatic_udp: UDP BitTorrent tracker"; +pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); pub fn run(config: Config) -> ::anyhow::Result<()> { let state = State::new(config.request_workers); diff --git a/aquatic_udp/src/main.rs b/aquatic_udp/src/main.rs index 4955653..b6df27c 100644 --- a/aquatic_udp/src/main.rs +++ b/aquatic_udp/src/main.rs @@ -4,6 +4,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; fn main() { aquatic_cli_helpers::run_app_with_cli_and_config::( aquatic_udp::APP_NAME, + aquatic_udp::APP_VERSION, aquatic_udp::run, None, ) diff --git a/aquatic_udp_bench/src/main.rs b/aquatic_udp_bench/src/main.rs index 153ccac..6f65bc9 100644 --- a/aquatic_udp_bench/src/main.rs +++ b/aquatic_udp_bench/src/main.rs @@ -31,6 +31,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; fn main() { run_app_with_cli_and_config::( "aquatic_udp_bench: Run aquatic_udp benchmarks", + env!("CARGO_PKG_VERSION"), run, None, ) diff --git a/aquatic_udp_load_test/src/main.rs b/aquatic_udp_load_test/src/main.rs index 2488659..9cdff4e 100644 --- a/aquatic_udp_load_test/src/main.rs +++ b/aquatic_udp_load_test/src/main.rs @@ -24,6 +24,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; pub fn main() { aquatic_cli_helpers::run_app_with_cli_and_config::( "aquatic_udp_load_test: BitTorrent load tester", + env!("CARGO_PKG_VERSION"), run, None, ) diff --git a/aquatic_ws/src/lib.rs b/aquatic_ws/src/lib.rs index 4b6c9ad..9e3deab 100644 --- a/aquatic_ws/src/lib.rs +++ b/aquatic_ws/src/lib.rs @@ -18,6 +18,7 @@ use common::*; use config::Config; pub const APP_NAME: &str = "aquatic_ws: WebTorrent tracker"; +pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const SHARED_IN_CHANNEL_SIZE: usize = 1024; diff --git a/aquatic_ws/src/main.rs b/aquatic_ws/src/main.rs index 204bf5c..132d7da 100644 --- a/aquatic_ws/src/main.rs +++ b/aquatic_ws/src/main.rs @@ -5,5 +5,5 @@ use aquatic_ws::config::Config; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; fn main() { - run_app_with_cli_and_config::(aquatic_ws::APP_NAME, aquatic_ws::run, None) + run_app_with_cli_and_config::(aquatic_ws::APP_NAME, aquatic_ws::APP_VERSION, aquatic_ws::run, None) } diff --git a/aquatic_ws_load_test/src/main.rs b/aquatic_ws_load_test/src/main.rs index 9626f58..78bc24d 100644 --- a/aquatic_ws_load_test/src/main.rs +++ b/aquatic_ws_load_test/src/main.rs @@ -23,6 +23,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; pub fn main() { aquatic_cli_helpers::run_app_with_cli_and_config::( "aquatic_ws_load_test: WebTorrent load tester", + env!("CARGO_PKG_VERSION"), run, None, )