feat(cli): add clap-verbosity-flag for enhanced logging control

This commit is contained in:
hdbg
2025-12-08 15:16:15 +01:00
parent be866cfb1a
commit a01f511311
6 changed files with 34 additions and 16 deletions

12
Cargo.lock generated
View File

@@ -219,6 +219,17 @@ dependencies = [
"clap_derive", "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]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.5.53" version = "4.5.53"
@@ -1091,6 +1102,7 @@ version = "0.0.2"
dependencies = [ dependencies = [
"bollard", "bollard",
"clap", "clap",
"clap-verbosity-flag",
"cliclack", "cliclack",
"colored", "colored",
"comfy-table", "comfy-table",

View File

@@ -9,6 +9,7 @@ license = "MIT"
[dependencies] [dependencies]
bollard = "0.19.4" bollard = "0.19.4"
clap = { version = "4.5.53", features = ["derive"] } clap = { version = "4.5.53", features = ["derive"] }
clap-verbosity-flag = { version = "3.0.4", features = ["tracing"] }
cliclack = "0.3.7" cliclack = "0.3.7"
colored = "3.0.0" colored = "3.0.0"
comfy-table = "7.2.1" comfy-table = "7.2.1"

View File

@@ -15,8 +15,8 @@ pub struct Cli {
#[command(subcommand)] #[command(subcommand)]
pub command: Commands, pub command: Commands,
#[arg(short, long, global = true)] #[command(flatten)]
pub verbose: bool, pub verbosity: clap_verbosity_flag::Verbosity,
} }
#[derive(Clone, clap::ValueEnum)] #[derive(Clone, clap::ValueEnum)]

View File

@@ -15,7 +15,7 @@ use colored::Colorize;
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use indicatif::MultiProgress; use indicatif::MultiProgress;
use miette::{Context, IntoDiagnostic, Result}; use miette::{Context, IntoDiagnostic, Result};
use tracing::info; use tracing::{debug, info};
use crate::{ use crate::{
config::PostgresVersion, config::PostgresVersion,
@@ -46,7 +46,7 @@ impl DockerController {
"Failed to connect to Docker! pgd required Docker installed. Make sure it's running.", "Failed to connect to Docker! pgd required Docker installed. Make sure it's running.",
)?; )?;
info!("docker.created"); debug!("Connected to docker!");
docker docker
.list_images(Some(ListImagesOptions::default())) .list_images(Some(ListImagesOptions::default()))
@@ -283,10 +283,7 @@ impl DockerController {
..Default::default() ..Default::default()
}); });
self.daemon
self
.daemon
.logs(container_id, options) .logs(container_id, options)
.map(|k| k.into_diagnostic().wrap_err("Failed streaming logs")) .map(|k| k.into_diagnostic().wrap_err("Failed streaming logs"))
} }

View File

@@ -5,6 +5,7 @@ use miette::{Diagnostic, bail};
use colored::Colorize; use colored::Colorize;
use miette::Result; use miette::Result;
use thiserror::Error; use thiserror::Error;
use tracing::info;
use crate::{ use crate::{
config::{PostgresVersion, Project}, config::{PostgresVersion, Project},
@@ -66,7 +67,7 @@ impl<'a> Reconciler<'a> {
.is_container_running_by_id(&container_id) .is_container_running_by_id(&container_id)
.await? .await?
{ {
println!("{}", "Container is already running".white()); info!("Container is already running");
return Ok(()); return Ok(());
} }
@@ -158,7 +159,7 @@ impl<'a> Reconciler<'a> {
} }
async fn update_project_container(&self, project: &Project) -> Result<String, miette::Error> { async fn update_project_container(&self, project: &Project) -> Result<String, miette::Error> {
println!( info!(
"{} {}", "{} {}",
"Creating container".cyan(), "Creating container".cyan(),
project.container_name().yellow() project.container_name().yellow()
@@ -173,7 +174,7 @@ impl<'a> Reconciler<'a> {
project.config.port, project.config.port,
) )
.await?; .await?;
println!("{}", "Container created successfully".green()); info!("{}", "Container created successfully".green());
self.ctx.state.set( self.ctx.state.set(
project.name.clone(), project.name.clone(),
crate::state::InstanceState::new( crate::state::InstanceState::new(

View File

@@ -6,10 +6,13 @@ mod consts;
mod controller; mod controller;
use std::env::args;
use clap::Parser; use clap::Parser;
use clap_verbosity_flag::Verbosity;
use cli::Cli; use cli::Cli;
use miette::Result; use miette::Result;
use tracing::info; use tracing::{debug, info};
use crate::{ use crate::{
cli::ControlCommands, cli::ControlCommands,
@@ -19,9 +22,9 @@ use crate::{
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
init_tracing(cli.verbose); init_tracing(cli.verbosity);
info!("pgd.start"); debug!("pgd.start");
match cli.command { match cli.command {
cli::Commands::Init => { cli::Commands::Init => {
@@ -50,6 +53,10 @@ async fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn init_tracing(_verbose: bool) { fn init_tracing(verbosity: Verbosity) {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt()
.with_max_level(verbosity)
.without_time()
.with_target(false)
.init();
} }