From 8726a1c6bf2081bb9352b3e9b5ed1b6fa26be272 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Wed, 6 Nov 2024 17:08:16 +1100 Subject: [PATCH] Add a verbose/quiet pair of flags --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/args.rs | 7 ++++++- src/main.rs | 9 ++++++--- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b98277a..f932c1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,6 +510,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" @@ -1294,6 +1304,7 @@ dependencies = [ "bon", "chrono", "clap", + "clap-verbosity-flag", "color-eyre", "dialoguer", "directories", diff --git a/Cargo.toml b/Cargo.toml index 0eefb13..f558d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ ignored = ["progenitor-client", "regress"] bon = "2.3.0" chrono = { version = "0.4.38", features = ["serde"] } clap = { version = "4.5.20", features = ["derive"] } +clap-verbosity-flag = "2.2.2" color-eyre = "0.6.3" dialoguer = "0.11.0" directories = "5.0.1" diff --git a/src/args.rs b/src/args.rs index db19b06..4cb8fe7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use clap::{command, Parser, Subcommand}; +use clap_verbosity_flag::{InfoLevel, Verbosity}; use serde::Serialize; #[derive(Parser, Serialize)] @@ -27,6 +28,10 @@ pub(crate) struct Opts { #[command(subcommand)] #[serde(flatten)] pub command: Commands, + + #[command(flatten)] + #[serde(skip)] + pub verbose: Verbosity, } #[derive(Serialize, Subcommand)] @@ -78,7 +83,7 @@ pub(crate) enum PeopleCommands { /// Synchronises date of births from a vcard file #[serde(rename = "sync_date_of_births")] SyncDateOfBirths { - #[arg(short, long)] + #[arg(long)] #[serde(skip_serializing_if = "::std::option::Option::is_none")] vcard: Option, }, diff --git a/src/main.rs b/src/main.rs index 46ce8c0..8c22c03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,12 @@ mod utils; #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; - pretty_env_logger::init(); + + let args = args::Opts::parse(); + + pretty_env_logger::formatted_timed_builder() + .filter_level(args.verbose.log_level_filter()) + .init(); let mut conf_extractor = Figment::new(); if let Some(project_dirs) = ProjectDirs::from("fr", "enoent", "Immich Tools") { @@ -53,8 +58,6 @@ async fn main() -> Result<()> { warn!("Unable to determine configuration file path"); } - let args = args::Opts::parse(); - let conf: Config = conf_extractor .merge(FileAdapter::wrap(Env::prefixed("IMMICH_TOOLS_"))) .merge(Serialized::defaults(&args)) -- 2.50.1