Skip to content

Commit

Permalink
feat(hok): add global --verbose flag
Browse files Browse the repository at this point in the history
and switch to use `tracing_subscriber` for enhanced logging

Signed-off-by: Chawye Hsu <[email protected]>
  • Loading branch information
chawyehsu committed Dec 10, 2024
1 parent 95c8937 commit 5fd0505
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 74 deletions.
142 changes: 110 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ path = "src/lib.rs"
[dependencies]
anyhow = "1.0"
clap = { version = "4.5", features = ["wrap_help", "cargo", "derive"] }
clap-verbosity-flag = { version = "3.0.1", features = ["tracing"], default-features = false }
clap_complete = "4.5.38"
crossterm = "0.28"
env_logger = "0.8.3"
indicatif = "0.17.5"
regex = "1.5.3"
remove_dir_all = "0.7.0"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }

[dependencies.libscoop]
version = "0.1.0-beta.7"
Expand Down
86 changes: 69 additions & 17 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use clap::{crate_description, crate_name, crate_version, Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use libscoop::Session;
use tracing_subscriber::{
filter::LevelFilter, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};

mod bucket;
mod cache;
Expand All @@ -18,13 +23,16 @@ mod update;
mod upgrade;

use crate::Result;
use libscoop::Session;

#[derive(Parser)]
#[command(
name = crate_name!(),
version = crate_version!(),
about = crate_description!(),
long_about = format!("{}
If you find any bugs or have a feature request, please open an issue on
GitHub: https://github.com/chawyehsu/hok/issues", crate_description!()),
subcommand_required = true,
arg_required_else_help = true,
max_term_width = 100,
Expand All @@ -36,6 +44,10 @@ use libscoop::Session;
pub struct Cli {
#[command(subcommand)]
pub command: Command,

/// The verbosity level
#[command(flatten)]
verbose: Verbosity,
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -63,25 +75,65 @@ pub enum Command {
}

/// CLI entry point
pub fn start(session: &Session) -> Result<()> {
pub fn start() -> Result<()> {
let args = Cli::parse();
setup_logger(args.verbose.tracing_level_filter())?;

let session = Session::default();
let user_agent = format!("Scoop/1.0 (+https://scoop.sh/) Hok/{}", crate_version!());
let _ = session.set_user_agent(&user_agent);

match args.command {
Command::Bucket(args) => bucket::execute(args, session),
Command::Cache(args) => cache::execute(args, session),
Command::Cat(args) => cat::execute(args, session),
Command::Cleanup(args) => cleanup::execute(args, session),
Command::Bucket(args) => bucket::execute(args, &session),
Command::Cache(args) => cache::execute(args, &session),
Command::Cat(args) => cat::execute(args, &session),
Command::Cleanup(args) => cleanup::execute(args, &session),
Command::Completions(args) => completions::execute(args),
Command::Config(args) => config::execute(args, session),
Command::Hold(args) => hold::execute(args, session),
Command::Home(args) => home::execute(args, session),
Command::Info(args) => info::execute(args, session),
Command::Install(args) => install::execute(args, session),
Command::List(args) => list::execute(args, session),
Command::Search(args) => search::execute(args, session),
Command::Unhold(args) => unhold::execute(args, session),
Command::Uninstall(args) => uninstall::execute(args, session),
Command::Update(args) => update::execute(args, session),
Command::Upgrade(args) => upgrade::execute(args, session),
Command::Config(args) => config::execute(args, &session),
Command::Hold(args) => hold::execute(args, &session),
Command::Home(args) => home::execute(args, &session),
Command::Info(args) => info::execute(args, &session),
Command::Install(args) => install::execute(args, &session),
Command::List(args) => list::execute(args, &session),
Command::Search(args) => search::execute(args, &session),
Command::Unhold(args) => unhold::execute(args, &session),
Command::Uninstall(args) => uninstall::execute(args, &session),
Command::Update(args) => update::execute(args, &session),
Command::Upgrade(args) => upgrade::execute(args, &session),
}
}

fn setup_logger(level_filter: LevelFilter) -> Result<()> {
// filter for low-level/depedency logs
let low_level_filter = match level_filter {
LevelFilter::OFF => LevelFilter::OFF,
LevelFilter::ERROR => LevelFilter::ERROR,
LevelFilter::WARN => LevelFilter::WARN,
LevelFilter::INFO => LevelFilter::WARN,
LevelFilter::DEBUG => LevelFilter::INFO,
LevelFilter::TRACE => LevelFilter::TRACE,
};

let mut layer_env_filter = EnvFilter::builder()
.with_default_directive(level_filter.into())
.from_env()?;

// The custom `HOK_LOG_LEVEL` environment variable was introduced to set the
// log level for hok since the first version.
if let Ok(level) = std::env::var("HOK_LOG_LEVEL") {
layer_env_filter = layer_env_filter.add_directive(format!("libscoop={level}").parse()?);
}

layer_env_filter = layer_env_filter
// add low-level filter for git2
.add_directive(format!("git2={}", low_level_filter).parse()?);

let layer_fmt = tracing_subscriber::fmt::layer().without_time();

tracing_subscriber::registry()
.with(layer_env_filter)
.with(layer_fmt)
.init();

Ok(())
}
Loading

0 comments on commit 5fd0505

Please sign in to comment.