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",
]
[[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",

View File

@@ -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"

View File

@@ -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)]

View File

@@ -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"))
}

View File

@@ -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<String, miette::Error> {
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(

View File

@@ -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();
}