Merge pull request #57 from greatest-ape/work-2022-03-24

Add command-line option for printing version and commit info; other fixes
This commit is contained in:
Joakim Frostegård 2022-03-24 16:45:51 +01:00 committed by GitHub
commit f38233c32f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 99 additions and 16 deletions

50
Cargo.lock generated
View file

@ -60,6 +60,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"aquatic_toml_config",
"git-testament",
"log",
"serde",
"simple_logger",
@ -344,9 +345,9 @@ dependencies = [
[[package]]
name = "async-tungstenite"
version = "0.17.1"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7922abeade7dd8948c20dfa1f85dc48cc952d2e0791f7c42b8b1cbb07a57129d"
checksum = "a1b71b31561643aa8e7df3effe284fa83ab1a840e52294c5f4bd7bfd8b2becbb"
dependencies = [
"futures-io",
"futures-util",
@ -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"
@ -1015,9 +1039,9 @@ checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "halfbrown"
version = "0.1.13"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49e26621a30b9fdb4f949b9c6a7fa42ce88112851c33ac4ca00bfa7848d26fb4"
checksum = "102968a036b06654b555049d9a6c4f46046805d1e1b22647720e93e0704d4c60"
dependencies = [
"hashbrown 0.12.0",
"serde",
@ -1225,9 +1249,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.14"
version = "0.4.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8"
dependencies = [
"cfg-if",
]
@ -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"
@ -2037,9 +2067,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.7"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
dependencies = [
"itoa 1.0.1",
"libc",
@ -2049,9 +2079,9 @@ dependencies = [
[[package]]
name = "time-macros"
version = "0.2.3"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
[[package]]
name = "tinytemplate"

View file

@ -6,7 +6,6 @@
* Use thin LTO?
* Add release-debug profile?
* newer glommio versions might use SIGUSR1 internally, see glommio fe33e30
* quit whole program if any thread panics
* config: fail on unrecognized keys?
* Run cargo-deny in CI

View file

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

View file

@ -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"] }

View file

@ -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<String>,
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<T>(
app_title: &str,
crate_version: &str,
// Function that takes config file and runs application
app_fn: fn(T) -> anyhow::Result<()>,
opts: Option<Options>,
) 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<T>(
fn run_inner<T>(
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::<T>());
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()
}

View file

@ -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;

View file

@ -5,5 +5,10 @@ use aquatic_http::config::Config;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
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() {
aquatic_cli_helpers::run_app_with_cli_and_config::<Config>(
"aquatic_http_load_test: BitTorrent load tester",
env!("CARGO_PKG_VERSION"),
run,
None,
)

View file

@ -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);

View file

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

View file

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

View file

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

View file

@ -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;

View file

@ -5,5 +5,10 @@ use aquatic_ws::config::Config;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
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() {
aquatic_cli_helpers::run_app_with_cli_and_config::<Config>(
"aquatic_ws_load_test: WebTorrent load tester",
env!("CARGO_PKG_VERSION"),
run,
None,
)