From 2fbac41ed636215dce8da39be37e11eec23d2b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Thu, 17 Mar 2022 15:25:59 +0100 Subject: [PATCH] chore: upgrade clap --- Cargo.lock | 140 +++++++++++++++++++++++++++++++++++--------------- Cargo.toml | 2 +- src/config.rs | 38 +++++++------- 3 files changed, 118 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b5ee78..c3285ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,15 +13,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - [[package]] name = "anyhow" version = "1.0.40" @@ -159,17 +150,33 @@ dependencies = [ [[package]] name = "clap" -version = "2.33.3" +version = "3.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +checksum = "d8c93436c21e4698bacadf42917db28b23017027a4deccb35dbe47a7e7840123" dependencies = [ - "ansi_term", "atty", "bitflags", - "strsim 0.8.0", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim", + "termcolor", + "terminal_size", "textwrap", - "unicode-width", - "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da95d038ede1a964ce99f49cbe27a7fb538d1da595e4b4f70b8c8f338d17bf16" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -216,7 +223,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim 0.10.0", + "strsim", "syn", ] @@ -368,6 +375,12 @@ dependencies = [ "ahash", ] +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -671,6 +684,15 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + [[package]] name = "parking_lot" version = "0.11.1" @@ -762,6 +784,30 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -776,9 +822,9 @@ checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" [[package]] name = "proc-macro2" -version = "1.0.26" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] @@ -978,12 +1024,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.10.0" @@ -992,9 +1032,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.70" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9505f307c872bab8eb46f77ae357c8eba1fdacead58ee5a850116b1d7f82883" +checksum = "ea297be220d52398dcc07ce15a209fce436d361735ac1db700cab3b6cdfb9f54" dependencies = [ "proc-macro2", "quote", @@ -1008,12 +1048,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8" [[package]] -name = "textwrap" -version = "0.11.0" +name = "termcolor" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" dependencies = [ - "unicode-width", + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" +dependencies = [ + "terminal_size", ] [[package]] @@ -1185,24 +1244,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" -[[package]] -name = "unicode-width" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" - [[package]] name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.4" @@ -1305,6 +1352,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 0a00078..109b59a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ description = "Pings configured hosts in a configurable intervals and exposes me toml = "0.5" futures = "0.3" tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] } -clap = "2" +clap = { version = "3", features = ["derive", "wrap_help"] } fern = "0.6" log = { version = "0.4", features = ["serde"] } chrono = "0.4" diff --git a/src/config.rs b/src/config.rs index f78a32c..dc3ce90 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ * along with this program. If not, see . * ********************************************************************************/ use anyhow::{Context, Result}; -use clap::{clap_app, crate_authors, crate_description, crate_name, crate_version}; +use clap::Parser; use log::info; use metrics::{describe_histogram, register_histogram, Unit}; use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle}; @@ -75,17 +75,20 @@ impl Default for LogConfig { } } -fn setup_clap() -> clap::ArgMatches<'static> { - clap_app!(myapp => - (name: crate_name!()) - (version: crate_version!()) - (author: crate_authors!()) - (about: crate_description!()) - (@arg config: +required "Set config file") - (@arg v: -v --verbose ... "Be verbose (you can add this up to 4 times for more logs). - By default, only errors are logged, so no output is a good thing.") - ) - .get_matches() +#[derive(clap::Parser)] +#[clap(author, version)] +/// Pings configured hosts in a configurable intervals and exposes metrics for prometheus. +pub struct Cli { + /// Set config file + config: String, + #[clap(short, long, parse(from_occurrences))] + /// Be verbose (you can add this up to 4 times for more logs). + /// By default, only errors are logged, so no output is a good thing. + verbose: usize, +} + +fn setup_clap() -> Cli { + Cli::parse() } fn setup_fern(level: log::LevelFilter) { @@ -115,12 +118,9 @@ fn read_config(path: &str) -> Result { } pub(crate) fn setup_app() -> Result { - let clap = setup_clap(); - let config_path = clap - .value_of("config") - .context("Got no config file. clap should've catched this")?; - let config = read_config(config_path).context("Couldn't read config file!")?; - setup_fern(determine_level(clap.occurrences_of("v"), config.log.level)); + let cli = setup_clap(); + let config = read_config(&cli.config).context("Couldn't read config file!")?; + setup_fern(determine_level(cli.verbose, config.log.level)); let handle = setup_prometheus(&config)?; Ok(App { config, handle }) } @@ -146,7 +146,7 @@ pub(crate) fn setup_prometheus(config: &Config) -> Result { Ok(handle) } -fn determine_level(verbose_occurrences: u64, config_level: log::LevelFilter) -> log::LevelFilter { +fn determine_level(verbose_occurrences: usize, config_level: log::LevelFilter) -> log::LevelFilter { let cli_level = match verbose_occurrences { 0 => log::LevelFilter::Error, 1 => log::LevelFilter::Warn,