Skip to content

Commit

Permalink
Add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
linyinfeng committed Sep 10, 2023
1 parent 2eb18a8 commit 1ae0299
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
let mut command = Command::new("nix");
command.args(args_vec);

log::debug!("run command: {:?}", command_vec);
log::trace!("run command: {:?}", command_vec);

let output = get_command_output(command)?;

Expand All @@ -138,7 +138,7 @@ where
stdout: String::from_utf8(output.stdout)?,
stderr: String::from_utf8(output.stderr)?,
};
log::debug!("command called:\n{}", process_info);
log::trace!("command called:\n{}", process_info);
if !output.status.success() {
return Err(Error::ProcessFailed(process_info));
}
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use error::Error;
use options::{Commands, Options};

fn main() {
init_logger();

let options = options::Options::parse();
init_logger(&options);
log::debug!("options = {:#?}", options);
if let Err(e) = main_result(options) {
log::error!("{}", e);
Expand All @@ -27,11 +26,16 @@ fn main_result(options: Options) -> Result<(), Error> {
Ok(())
}

fn init_logger() {
fn init_logger(options: &Options) {
let mut builder = pretty_env_logger::formatted_builder();
let default_level = match options.verbose {
0 => "info",
1 => "debug",
_ => "trace",
};
let filters = match std::env::var("RUST_LOG") {
Ok(f) => f,
Err(_) => "flat_flake=info".to_string(),
Err(_) => format!("flat_flake={default_level}"),
};
builder.parse_filters(&filters);
builder.init()
Expand Down
3 changes: 3 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use clap::{Parser, Subcommand};
pub struct Options {
#[command(subcommand)]
pub command: Commands,
#[arg(short, long, global = true, action = clap::ArgAction::Count,
help = "Increase logging verbosity, override by RUST_LOG")]
pub verbose: u8,
}

#[derive(Clone, Debug, Subcommand)]
Expand Down

0 comments on commit 1ae0299

Please sign in to comment.