Add command-line option for printing version and commit info

This commit is contained in:
Joakim Frostegård 2022-03-24 16:17:17 +01:00
parent bdc719b755
commit f5b1cd5525
14 changed files with 79 additions and 5 deletions

30
Cargo.lock generated
View file

@ -60,6 +60,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"aquatic_toml_config", "aquatic_toml_config",
"git-testament",
"log", "log",
"serde", "serde",
"simple_logger", "simple_logger",
@ -962,6 +963,29 @@ version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" 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]] [[package]]
name = "glommio" name = "glommio"
version = "0.7.0" version = "0.7.0"
@ -1338,6 +1362,12 @@ dependencies = [
"memoffset 0.6.5", "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]] [[package]]
name = "nodrop" name = "nodrop"
version = "0.1.14" version = "0.1.14"

View file

@ -43,16 +43,19 @@ fn run() -> Result<(), Option<String>> {
match protocol.as_str() { match protocol.as_str() {
"udp" => run_app_with_cli_and_config::<UdpConfig>( "udp" => run_app_with_cli_and_config::<UdpConfig>(
aquatic_udp::APP_NAME, aquatic_udp::APP_NAME,
aquatic_udp::APP_VERSION,
aquatic_udp::run, aquatic_udp::run,
Some(options), Some(options),
), ),
"http" => run_app_with_cli_and_config::<HttpConfig>( "http" => run_app_with_cli_and_config::<HttpConfig>(
aquatic_http::APP_NAME, aquatic_http::APP_NAME,
aquatic_http::APP_VERSION,
aquatic_http::run, aquatic_http::run,
Some(options), Some(options),
), ),
"ws" => run_app_with_cli_and_config::<WsConfig>( "ws" => run_app_with_cli_and_config::<WsConfig>(
aquatic_ws::APP_NAME, aquatic_ws::APP_NAME,
aquatic_ws::APP_VERSION,
aquatic_ws::run, aquatic_ws::run,
Some(options), Some(options),
), ),

View file

@ -11,6 +11,7 @@ repository = "https://github.com/greatest-ape/aquatic"
aquatic_toml_config = "0.1.0" aquatic_toml_config = "0.1.0"
anyhow = "1" anyhow = "1"
git-testament = "0.2"
log = "0.4" log = "0.4"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
simple_logger = { version = "2", features = ["stderr"] } simple_logger = { version = "2", features = ["stderr"] }

View file

@ -3,6 +3,7 @@ use std::io::Read;
use anyhow::Context; use anyhow::Context;
use aquatic_toml_config::TomlConfig; use aquatic_toml_config::TomlConfig;
use git_testament::{git_testament, CommitKind};
use log::LevelFilter; use log::LevelFilter;
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
@ -35,6 +36,7 @@ pub trait Config: Default + TomlConfig + DeserializeOwned {
pub struct Options { pub struct Options {
config_file: Option<String>, config_file: Option<String>,
print_config: bool, print_config: bool,
print_version: bool,
} }
impl Options { impl Options {
@ -57,6 +59,9 @@ impl Options {
"-p" | "--print-config" => { "-p" | "--print-config" => {
options.print_config = true; options.print_config = true;
} }
"-v" | "--version" => {
options.print_version = true;
}
"-h" | "--help" => { "-h" | "--help" => {
return Err(None); return Err(None);
} }
@ -75,13 +80,14 @@ impl Options {
pub fn run_app_with_cli_and_config<T>( pub fn run_app_with_cli_and_config<T>(
app_title: &str, app_title: &str,
crate_version: &str,
// Function that takes config file and runs application // Function that takes config file and runs application
app_fn: fn(T) -> anyhow::Result<()>, app_fn: fn(T) -> anyhow::Result<()>,
opts: Option<Options>, opts: Option<Options>,
) where ) where
T: Config, 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, Ok(()) => 0,
Err(err) => { Err(err) => {
eprintln!("Error: {:#}", err); eprintln!("Error: {:#}", err);
@ -93,6 +99,7 @@ pub fn run_app_with_cli_and_config<T>(
fn run_inner<T>( fn run_inner<T>(
app_title: &str, app_title: &str,
crate_version: &str,
// Function that takes config file and runs application // Function that takes config file and runs application
app_fn: fn(T) -> anyhow::Result<()>, app_fn: fn(T) -> anyhow::Result<()>,
// Possibly preparsed options // 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::<T>()); print!("{}", default_config_as_toml::<T>());
Ok(()) Ok(())
@ -147,8 +160,9 @@ where
println!("\nOptions:"); println!("\nOptions:");
println!(" -c, --config-file Load config from this path"); println!(" -c, --config-file Load config from this path");
println!(" -p, --print-config Print default config");
println!(" -h, --help Print this help message"); 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 { if let Some(error) = opt_error {
println!("\nError: {}.", error); println!("\nError: {}.", error);
@ -195,3 +209,21 @@ fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> {
Ok(()) 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()
}

View file

@ -19,6 +19,7 @@ pub mod config;
mod workers; mod workers;
pub const APP_NAME: &str = "aquatic_http: BitTorrent tracker (HTTP over TLS)"; 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; const SHARED_CHANNEL_SIZE: usize = 1024;

View file

@ -5,5 +5,5 @@ use aquatic_http::config::Config;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() { fn main() {
run_app_with_cli_and_config::<Config>(aquatic_http::APP_NAME, aquatic_http::run, None) run_app_with_cli_and_config::<Config>(aquatic_http::APP_NAME, aquatic_http::APP_VERSION, aquatic_http::run, None)
} }

View file

@ -26,6 +26,7 @@ const MBITS_FACTOR: f64 = 1.0 / ((1024.0 * 1024.0) / 8.0);
pub fn main() { pub fn main() {
aquatic_cli_helpers::run_app_with_cli_and_config::<Config>( aquatic_cli_helpers::run_app_with_cli_and_config::<Config>(
"aquatic_http_load_test: BitTorrent load tester", "aquatic_http_load_test: BitTorrent load tester",
env!("CARGO_PKG_VERSION"),
run, run,
None, None,
) )

View file

@ -23,6 +23,7 @@ use common::{ConnectedRequestSender, ConnectedResponseSender, SocketWorkerIndex,
use crate::common::RequestWorkerIndex; use crate::common::RequestWorkerIndex;
pub const APP_NAME: &str = "aquatic_udp: UDP BitTorrent tracker"; 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<()> { pub fn run(config: Config) -> ::anyhow::Result<()> {
let state = State::new(config.request_workers); let state = State::new(config.request_workers);

View file

@ -4,6 +4,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() { fn main() {
aquatic_cli_helpers::run_app_with_cli_and_config::<aquatic_udp::config::Config>( aquatic_cli_helpers::run_app_with_cli_and_config::<aquatic_udp::config::Config>(
aquatic_udp::APP_NAME, aquatic_udp::APP_NAME,
aquatic_udp::APP_VERSION,
aquatic_udp::run, aquatic_udp::run,
None, None,
) )

View file

@ -31,6 +31,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() { fn main() {
run_app_with_cli_and_config::<BenchConfig>( run_app_with_cli_and_config::<BenchConfig>(
"aquatic_udp_bench: Run aquatic_udp benchmarks", "aquatic_udp_bench: Run aquatic_udp benchmarks",
env!("CARGO_PKG_VERSION"),
run, run,
None, None,
) )

View file

@ -24,6 +24,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
pub fn main() { pub fn main() {
aquatic_cli_helpers::run_app_with_cli_and_config::<Config>( aquatic_cli_helpers::run_app_with_cli_and_config::<Config>(
"aquatic_udp_load_test: BitTorrent load tester", "aquatic_udp_load_test: BitTorrent load tester",
env!("CARGO_PKG_VERSION"),
run, run,
None, None,
) )

View file

@ -18,6 +18,7 @@ use common::*;
use config::Config; use config::Config;
pub const APP_NAME: &str = "aquatic_ws: WebTorrent tracker"; 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; pub const SHARED_IN_CHANNEL_SIZE: usize = 1024;

View file

@ -5,5 +5,5 @@ use aquatic_ws::config::Config;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() { fn main() {
run_app_with_cli_and_config::<Config>(aquatic_ws::APP_NAME, aquatic_ws::run, None) run_app_with_cli_and_config::<Config>(aquatic_ws::APP_NAME, aquatic_ws::APP_VERSION, aquatic_ws::run, None)
} }

View file

@ -23,6 +23,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
pub fn main() { pub fn main() {
aquatic_cli_helpers::run_app_with_cli_and_config::<Config>( aquatic_cli_helpers::run_app_with_cli_and_config::<Config>(
"aquatic_ws_load_test: WebTorrent load tester", "aquatic_ws_load_test: WebTorrent load tester",
env!("CARGO_PKG_VERSION"),
run, run,
None, None,
) )