From 442601f25a93a9ef9f0a151130fe3fa945b7ec7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Sat, 26 Oct 2024 13:23:22 +0200 Subject: [PATCH] feat: add -v verbosity flag --- Cargo.lock | 13 ++++++++++++- Cargo.toml | 25 +++++++++++++------------ src/main.rs | 26 +++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b265d1..32c6c17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -237,6 +237,16 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-verbosity-flag" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e099138e1807662ff75e2cebe4ae2287add879245574489f9b1588eb5e5564ed" +dependencies = [ + "clap", + "log", +] + [[package]] name = "clap_builder" version = "4.5.20" @@ -1159,6 +1169,7 @@ dependencies = [ "axum-client-ip", "base64 0.22.1", "clap", + "clap-verbosity-flag", "http", "insta", "miette", diff --git a/Cargo.toml b/Cargo.toml index 1622810..17fef08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,27 +7,28 @@ version = "0.3.0-dev" edition = "2021" [dependencies] -axum = "0.7.7" -axum-auth = { version = "0.7.0", default-features = false, features = [ +axum = "0.7" +axum-auth = { version = "0.7", default-features = false, features = [ "auth-basic", ] } -axum-client-ip = "0.6.1" -base64 = "0.22.1" -clap = { version = "4.5.20", features = ["derive", "env"] } -http = "1.1.0" -miette = { version = "7.2.0", features = ["fancy"] } -ring = { version = "0.17.8", features = ["std"] } -tokio = { version = "1.40.0", features = [ +axum-client-ip = "0.6" +base64 = "0.22" +clap = { version = "4", features = ["derive", "env"] } +clap-verbosity-flag = "2" +http = "1" +miette = { version = "7", features = ["fancy"] } +ring = { version = "0.17", features = ["std"] } +tokio = { version = "1", features = [ "macros", "rt", "process", "io-util", ] } -tracing = "0.1.40" -tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } [dev-dependencies] -insta = "1.40.0" +insta = "1" [profile.dev] debug = 0 diff --git a/src/main.rs b/src/main.rs index 19c1e66..7e2ffae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use axum_auth::AuthBasic; use axum_client_ip::{SecureClientIp, SecureClientIpSource}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use clap::{Parser, Subcommand}; +use clap_verbosity_flag::Verbosity; use http::StatusCode; use miette::{bail, ensure, Context, IntoDiagnostic, Result}; use tokio::io::AsyncWriteExt; @@ -26,6 +27,9 @@ const DEFAULT_SALT: &str = "UpdateMyDNS"; #[derive(Debug, Parser)] struct Opts { + #[command(flatten)] + verbosity: Verbosity, + /// Ip address of the server #[arg(long, default_value = "127.0.0.1")] address: IpAddr, @@ -121,6 +125,7 @@ struct AppState<'a> { } fn load_ip(path: &Path) -> Result> { + debug!("loading last IP from {}", path.display()); let data = match std::fs::read_to_string(path) { Ok(ip) => ip, Err(err) => { @@ -146,13 +151,28 @@ 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(LevelFilter::WARN.into()) + .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(), + ) .from_env_lossy(), ) .finish(); @@ -166,6 +186,7 @@ fn main() -> Result<()> { } let Opts { + verbosity: _, address: ip, port, password_file, @@ -287,6 +308,7 @@ async fn update_records( AuthBasic((username, pass)): AuthBasic, SecureClientIp(ip): SecureClientIp, ) -> axum::response::Result<&'static str> { + debug!("received update request from {ip}"); let Some(pass) = pass else { return Err((StatusCode::UNAUTHORIZED, Json::from("no password provided")).into()); }; @@ -309,9 +331,11 @@ async fn update_records( match nsupdate(ip, state.ttl, state.key_file, state.records).await { Ok(status) if status.success() => { tokio::task::spawn_blocking(move || { + info!("updating last ip to {ip}"); if let Err(err) = std::fs::write(state.ip_file, format!("{ip}")) { error!("Failed to update last IP: {err}"); } + info!("updated last ip to {ip}"); }); Ok("successful update") }