feat: upgrade clap_verbosity_flag
Adds tracing support which simplyfies code.
This commit is contained in:
parent
d98c4202f4
commit
60aed649b1
3 changed files with 12 additions and 28 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -239,12 +239,12 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "clap-verbosity-flag"
|
||||
version = "2.2.3"
|
||||
version = "3.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34c77f67047557f62582784fd7482884697731b2932c7d37ced54bce2312e1e2"
|
||||
checksum = "42dcf89bb9d98abb02e9a4a8ef1cce429e547a803460a8245c399860985d5281"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"log",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
11
Cargo.toml
11
Cargo.toml
|
@ -14,16 +14,13 @@ axum-auth = { version = "0.7", default-features = false, features = [
|
|||
axum-client-ip = "0.6"
|
||||
base64 = "0.22"
|
||||
clap = { version = "4", features = ["derive", "env"] }
|
||||
clap-verbosity-flag = "2"
|
||||
clap-verbosity-flag = { version = "3", default-features = false, features = [
|
||||
"tracing",
|
||||
] }
|
||||
http = "1"
|
||||
miette = { version = "7", features = ["fancy"] }
|
||||
ring = { version = "0.17", features = ["std"] }
|
||||
tokio = { version = "1", features = [
|
||||
"macros",
|
||||
"rt",
|
||||
"process",
|
||||
"io-util",
|
||||
] }
|
||||
tokio = { version = "1", features = ["macros", "rt", "process", "io-util"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -16,7 +16,7 @@ use clap_verbosity_flag::Verbosity;
|
|||
use http::StatusCode;
|
||||
use miette::{bail, ensure, Context, IntoDiagnostic, Result};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing::{debug, error, info, level_filters::LevelFilter, trace, warn};
|
||||
use tracing::{debug, error, info, trace, warn};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
mod password;
|
||||
|
@ -28,7 +28,7 @@ const DEFAULT_SALT: &str = "UpdateMyDNS";
|
|||
#[derive(Debug, Parser)]
|
||||
struct Opts {
|
||||
#[command(flatten)]
|
||||
verbosity: Verbosity,
|
||||
verbosity: Verbosity<clap_verbosity_flag::WarnLevel>,
|
||||
|
||||
/// Ip address of the server
|
||||
#[arg(long, default_value = "127.0.0.1")]
|
||||
|
@ -151,28 +151,13 @@ fn main() -> Result<()> {
|
|||
|
||||
// parse cli arguments
|
||||
let mut args = Opts::parse();
|
||||
debug!("{args:?}");
|
||||
|
||||
// configure logger
|
||||
let subscriber = tracing_subscriber::FmtSubscriber::builder()
|
||||
.without_time()
|
||||
.with_env_filter(
|
||||
EnvFilter::builder()
|
||||
.with_default_directive(
|
||||
if args.verbosity.is_present() {
|
||||
match args.verbosity.log_level_filter() {
|
||||
clap_verbosity_flag::LevelFilter::Off => LevelFilter::OFF,
|
||||
clap_verbosity_flag::LevelFilter::Error => LevelFilter::ERROR,
|
||||
clap_verbosity_flag::LevelFilter::Warn => LevelFilter::WARN,
|
||||
clap_verbosity_flag::LevelFilter::Info => LevelFilter::INFO,
|
||||
clap_verbosity_flag::LevelFilter::Debug => LevelFilter::DEBUG,
|
||||
clap_verbosity_flag::LevelFilter::Trace => LevelFilter::TRACE,
|
||||
}
|
||||
} else {
|
||||
LevelFilter::WARN
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.with_default_directive(args.verbosity.tracing_level_filter().into())
|
||||
.from_env_lossy(),
|
||||
)
|
||||
.finish();
|
||||
|
@ -180,6 +165,8 @@ fn main() -> Result<()> {
|
|||
.into_diagnostic()
|
||||
.wrap_err("setting global tracing subscriber")?;
|
||||
|
||||
debug!("{args:?}");
|
||||
|
||||
// process subcommand
|
||||
if let Some(cmd) = args.subcommand.take() {
|
||||
return cmd.process(&args);
|
||||
|
|
Loading…
Reference in a new issue