feat: upgrade clap_verbosity_flag
All checks were successful
/ build (push) Successful in 2s
/ check (push) Successful in 7s

Adds tracing support which simplyfies code.
This commit is contained in:
Jalil David Salamé Messina 2024-11-23 13:09:26 +01:00
parent d98c4202f4
commit 60aed649b1
Signed by: jalil
GPG key ID: F016B9E770737A0B
3 changed files with 12 additions and 28 deletions

6
Cargo.lock generated
View file

@ -239,12 +239,12 @@ dependencies = [
[[package]] [[package]]
name = "clap-verbosity-flag" name = "clap-verbosity-flag"
version = "2.2.3" version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34c77f67047557f62582784fd7482884697731b2932c7d37ced54bce2312e1e2" checksum = "42dcf89bb9d98abb02e9a4a8ef1cce429e547a803460a8245c399860985d5281"
dependencies = [ dependencies = [
"clap", "clap",
"log", "tracing-core",
] ]
[[package]] [[package]]

View file

@ -14,16 +14,13 @@ axum-auth = { version = "0.7", default-features = false, features = [
axum-client-ip = "0.6" axum-client-ip = "0.6"
base64 = "0.22" base64 = "0.22"
clap = { version = "4", features = ["derive", "env"] } clap = { version = "4", features = ["derive", "env"] }
clap-verbosity-flag = "2" clap-verbosity-flag = { version = "3", default-features = false, features = [
"tracing",
] }
http = "1" http = "1"
miette = { version = "7", features = ["fancy"] } miette = { version = "7", features = ["fancy"] }
ring = { version = "0.17", features = ["std"] } ring = { version = "0.17", features = ["std"] }
tokio = { version = "1", features = [ tokio = { version = "1", features = ["macros", "rt", "process", "io-util"] }
"macros",
"rt",
"process",
"io-util",
] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }

View file

@ -16,7 +16,7 @@ use clap_verbosity_flag::Verbosity;
use http::StatusCode; use http::StatusCode;
use miette::{bail, ensure, Context, IntoDiagnostic, Result}; use miette::{bail, ensure, Context, IntoDiagnostic, Result};
use tokio::io::AsyncWriteExt; 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; use tracing_subscriber::EnvFilter;
mod password; mod password;
@ -28,7 +28,7 @@ const DEFAULT_SALT: &str = "UpdateMyDNS";
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Opts { struct Opts {
#[command(flatten)] #[command(flatten)]
verbosity: Verbosity, verbosity: Verbosity<clap_verbosity_flag::WarnLevel>,
/// Ip address of the server /// Ip address of the server
#[arg(long, default_value = "127.0.0.1")] #[arg(long, default_value = "127.0.0.1")]
@ -151,28 +151,13 @@ fn main() -> Result<()> {
// parse cli arguments // parse cli arguments
let mut args = Opts::parse(); let mut args = Opts::parse();
debug!("{args:?}");
// configure logger // configure logger
let subscriber = tracing_subscriber::FmtSubscriber::builder() let subscriber = tracing_subscriber::FmtSubscriber::builder()
.without_time() .without_time()
.with_env_filter( .with_env_filter(
EnvFilter::builder() EnvFilter::builder()
.with_default_directive( .with_default_directive(args.verbosity.tracing_level_filter().into())
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(),
)
.from_env_lossy(), .from_env_lossy(),
) )
.finish(); .finish();
@ -180,6 +165,8 @@ fn main() -> Result<()> {
.into_diagnostic() .into_diagnostic()
.wrap_err("setting global tracing subscriber")?; .wrap_err("setting global tracing subscriber")?;
debug!("{args:?}");
// process subcommand // process subcommand
if let Some(cmd) = args.subcommand.take() { if let Some(cmd) = args.subcommand.take() {
return cmd.process(&args); return cmd.process(&args);