From a01f511311f72c94cf0ee6f4f0acf5ad718b9fa7 Mon Sep 17 00:00:00 2001 From: hdbg Date: Mon, 8 Dec 2025 15:16:15 +0100 Subject: [PATCH] feat(cli): add clap-verbosity-flag for enhanced logging control --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/cli.rs | 4 ++-- src/controller/docker.rs | 9 +++------ src/controller/reconciler.rs | 7 ++++--- src/main.rs | 17 ++++++++++++----- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bdb8c95..14c9228 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -219,6 +219,17 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-verbosity-flag" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d92b1fab272fe943881b77cc6e920d6543e5b1bfadbd5ed81c7c5a755742394" +dependencies = [ + "clap", + "log", + "tracing-core", +] + [[package]] name = "clap_builder" version = "4.5.53" @@ -1091,6 +1102,7 @@ version = "0.0.2" dependencies = [ "bollard", "clap", + "clap-verbosity-flag", "cliclack", "colored", "comfy-table", diff --git a/Cargo.toml b/Cargo.toml index 4d0f751..488f5f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT" [dependencies] bollard = "0.19.4" clap = { version = "4.5.53", features = ["derive"] } +clap-verbosity-flag = { version = "3.0.4", features = ["tracing"] } cliclack = "0.3.7" colored = "3.0.0" comfy-table = "7.2.1" diff --git a/src/cli.rs b/src/cli.rs index 59ed471..41e20f0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -15,8 +15,8 @@ pub struct Cli { #[command(subcommand)] pub command: Commands, - #[arg(short, long, global = true)] - pub verbose: bool, + #[command(flatten)] + pub verbosity: clap_verbosity_flag::Verbosity, } #[derive(Clone, clap::ValueEnum)] diff --git a/src/controller/docker.rs b/src/controller/docker.rs index f6e561c..63e2c64 100644 --- a/src/controller/docker.rs +++ b/src/controller/docker.rs @@ -15,7 +15,7 @@ use colored::Colorize; use futures::{Stream, StreamExt}; use indicatif::MultiProgress; use miette::{Context, IntoDiagnostic, Result}; -use tracing::info; +use tracing::{debug, info}; use crate::{ config::PostgresVersion, @@ -46,7 +46,7 @@ impl DockerController { "Failed to connect to Docker! pgd required Docker installed. Make sure it's running.", )?; - info!("docker.created"); + debug!("Connected to docker!"); docker .list_images(Some(ListImagesOptions::default())) @@ -283,10 +283,7 @@ impl DockerController { ..Default::default() }); - - - self - .daemon + self.daemon .logs(container_id, options) .map(|k| k.into_diagnostic().wrap_err("Failed streaming logs")) } diff --git a/src/controller/reconciler.rs b/src/controller/reconciler.rs index 6aff31e..02ac686 100644 --- a/src/controller/reconciler.rs +++ b/src/controller/reconciler.rs @@ -5,6 +5,7 @@ use miette::{Diagnostic, bail}; use colored::Colorize; use miette::Result; use thiserror::Error; +use tracing::info; use crate::{ config::{PostgresVersion, Project}, @@ -66,7 +67,7 @@ impl<'a> Reconciler<'a> { .is_container_running_by_id(&container_id) .await? { - println!("{}", "Container is already running".white()); + info!("Container is already running"); return Ok(()); } @@ -158,7 +159,7 @@ impl<'a> Reconciler<'a> { } async fn update_project_container(&self, project: &Project) -> Result { - println!( + info!( "{} {}", "Creating container".cyan(), project.container_name().yellow() @@ -173,7 +174,7 @@ impl<'a> Reconciler<'a> { project.config.port, ) .await?; - println!("{}", "Container created successfully".green()); + info!("{}", "Container created successfully".green()); self.ctx.state.set( project.name.clone(), crate::state::InstanceState::new( diff --git a/src/main.rs b/src/main.rs index 059e586..d90763b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,13 @@ mod consts; mod controller; +use std::env::args; + use clap::Parser; +use clap_verbosity_flag::Verbosity; use cli::Cli; use miette::Result; -use tracing::info; +use tracing::{debug, info}; use crate::{ cli::ControlCommands, @@ -19,9 +22,9 @@ use crate::{ #[tokio::main] async fn main() -> Result<()> { let cli = Cli::parse(); - init_tracing(cli.verbose); + init_tracing(cli.verbosity); - info!("pgd.start"); + debug!("pgd.start"); match cli.command { cli::Commands::Init => { @@ -50,6 +53,10 @@ async fn main() -> Result<()> { Ok(()) } -fn init_tracing(_verbose: bool) { - tracing_subscriber::fmt::init(); +fn init_tracing(verbosity: Verbosity) { + tracing_subscriber::fmt() + .with_max_level(verbosity) + .without_time() + .with_target(false) + .init(); }