Skip to content

Commit

Permalink
Add a --quiet option; send log messages to stderr
Browse files Browse the repository at this point in the history
Fixes #97.
  • Loading branch information
bradlarsen committed Nov 27, 2023
1 parent 617b6cb commit ed0373b
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- React App Username
- React App Password

- A new global `--quiet` / `-q` option has been added, which suppresses non-error feedback messages and disables progress bars.

### Fixes
- Command-line parameters that can meaningfully accept negative numbers can now be specified without having to use `--PARAMETER=NEGATIVE_VALUE` syntax; a space can now separate the paraemter and the value.

Expand Down Expand Up @@ -92,6 +94,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- `md5crypt Hash` (id `np.md5.1`) has been renamed to `Password Hash (md5crypt)` and re-identified as `np.pwhash.1`.
- `bcrypt Hash` (id `np.bcrypt.1`) has been renamed to `Password Hash (bcrypt)` and re-identified as `np.pwhash.2`.

- Log messages are written to stderr instead of stdout.

## [v0.15.0](https://github.com/praetorian-inc/noseyparker/releases/v0.15.0) (2023-10-12)

Expand Down
20 changes: 16 additions & 4 deletions crates/noseyparker-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ impl CommandLineArgs {
args.global_args.color = Mode::Never
}

// If `--quiet` is specified, disable progress bars
if args.global_args.quiet {
args.global_args.progress = Mode::Never;
}

args
}
}
Expand Down Expand Up @@ -191,17 +196,24 @@ pub struct GlobalArgs {
#[arg(global=true, long, short, action=ArgAction::Count)]
pub verbose: u8,

/// Suppress non-error feedback messages
///
/// This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars.
/// This overrides any provided verbosity and progress reporting options.
#[arg(global=true, long, short)]
pub quiet: bool,

/// Enable or disable colored output
///
/// When this is "auto", colors are enabled when stdout is a tty.
/// When this is "auto", colors are enabled for stdout and stderr when they are terminals.
///
/// If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to `--color=never`.
#[arg(global=true, long, default_value_t=Mode::Auto, value_name="MODE")]
pub color: Mode,

/// Enable or disable progress bars
///
/// When this is "auto", progress bars are enabled when stderr is a tty.
/// When this is "auto", progress bars are enabled when stderr is a terminal.
#[arg(global=true, long, default_value_t=Mode::Auto, value_name="MODE")]
pub progress: Mode,

Expand Down Expand Up @@ -243,11 +255,11 @@ pub struct AdvancedArgs {
}

impl GlobalArgs {
pub fn use_color(&self) -> bool {
pub fn use_color<T: IsTerminal>(&self, out: T) -> bool {
match self.color {
Mode::Never => false,
Mode::Always => true,
Mode::Auto => std::io::stdout().is_terminal(),
Mode::Auto => out.is_terminal(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/noseyparker-cli/src/cmd_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ pub fn run(global_args: &args::GlobalArgs, args: &args::ScanArgs) -> Result<()>
let matches_summary = datastore.summarize()?;
let matches_table = crate::cmd_summarize::summary_table(&matches_summary);
println!();
matches_table.print_tty(global_args.use_color())?;
matches_table.print_tty(global_args.use_color(std::io::stdout()))?;
}

println!("\nRun the `report` command next to show finding details.");
Expand Down
30 changes: 21 additions & 9 deletions crates/noseyparker-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,47 @@ mod util;

use args::GlobalArgs;

/// Set up the logging / tracing system for the application.
fn configure_tracing(global_args: &GlobalArgs) -> Result<()> {
use tracing_log::{AsLog, LogTracer};
use tracing_subscriber::{filter::LevelFilter, EnvFilter};

let level_filter = match global_args.verbose {
0 => LevelFilter::WARN,
1 => LevelFilter::INFO,
2 => LevelFilter::DEBUG,
_ => LevelFilter::TRACE,
// Set the tracing level according to the `-q`/`--quiet` and `-v`/`--verbose` options
let level_filter = if global_args.quiet {
LevelFilter::ERROR
} else {
match global_args.verbose {
0 => LevelFilter::WARN,
1 => LevelFilter::INFO,
2 => LevelFilter::DEBUG,
_ => LevelFilter::TRACE,
}
};

// Configure the bridge from the `log` crate to the `tracing` crate
LogTracer::builder()
.with_max_level(level_filter.as_log())
.init()?;

// Configure logging filters according to the `NP_LOG` environment variable
let env_filter = EnvFilter::builder()
.with_default_directive(level_filter.into())
.with_env_var("NP_LOG")
.from_env()
.context("Failed to parse filters from NP_LOG environment variable")?;

// Install the global tracing subscriber
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_ansi(global_args.use_color())
.with_ansi(global_args.use_color(std::io::stderr()))
.with_env_filter(env_filter)
.with_writer(std::io::stderr)
.finish();
tracing::subscriber::set_global_default(subscriber)?;

Ok(())
}

/// Set the process rlimits according to the global arguments.
fn configure_rlimits(global_args: &GlobalArgs) -> Result<()> {
use rlimit::Resource;
use std::cmp::max;
Expand All @@ -62,12 +73,13 @@ fn configure_rlimits(global_args: &GlobalArgs) -> Result<()> {
Ok(())
}

/// Enable or disable colored output according to the global arguments.
fn configure_color(global_args: &GlobalArgs) {
let use_color = global_args.use_color();
console::set_colors_enabled(use_color);
console::set_colors_enabled_stderr(use_color);
console::set_colors_enabled(global_args.use_color(std::io::stdout()));
console::set_colors_enabled_stderr(global_args.use_color(std::io::stderr()));
}

/// Enable or disable backtraces for the process according to the global arguments.
fn configure_backtraces(global_args: &GlobalArgs) {
if global_args.advanced.enable_backtraces {
// Print a stack trace in case of panic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -44,7 +50,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -34,7 +40,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -47,7 +53,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -34,7 +40,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: tests/test_noseyparker_help.rs
source: crates/noseyparker-cli/tests/help/mod.rs
expression: stdout
---
Interact with GitHub repositories
Expand All @@ -15,6 +15,7 @@ Options:

Global Options:
-v, --verbose... Enable verbose output
-q, --quiet Suppress non-error feedback messages
--color <MODE> Enable or disable colored output [default: auto] [possible values: auto,
never, always]
--progress <MODE> Enable or disable progress bars [default: auto] [possible values: auto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Options:

Global Options:
-v, --verbose... Enable verbose output
-q, --quiet Suppress non-error feedback messages
--color <MODE> Enable or disable colored output [default: auto] [possible values: auto,
never, always]
--progress <MODE> Enable or disable progress bars [default: auto] [possible values: auto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -60,7 +66,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Output Options:

Global Options:
-v, --verbose... Enable verbose output
-q, --quiet Suppress non-error feedback messages
--color <MODE> Enable or disable colored output [default: auto] [possible values: auto,
never, always]
--progress <MODE> Enable or disable progress bars [default: auto] [possible values: auto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -35,7 +41,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ Global Options:

This can be repeated up to 3 times to enable successively more output.

-q, --quiet
Suppress non-error feedback messages

This silences WARNING, INFO, DEBUG, and TRACE messages and disables progress bars. This
overrides any provided verbosity and progress reporting options.

--color <MODE>
Enable or disable colored output

When this is "auto", colors are enabled when stdout is a tty.
When this is "auto", colors are enabled for stdout and stderr when they are terminals.

If the `NO_COLOR` environment variable is set, it takes precedence and is equivalent to
`--color=never`.
Expand All @@ -224,7 +230,7 @@ Global Options:
--progress <MODE>
Enable or disable progress bars

When this is "auto", progress bars are enabled when stderr is a tty.
When this is "auto", progress bars are enabled when stderr is a terminal.

[default: auto]
[possible values: auto, never, always]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Data Collection Options:

Global Options:
-v, --verbose... Enable verbose output
-q, --quiet Suppress non-error feedback messages
--color <MODE> Enable or disable colored output [default: auto] [possible values: auto,
never, always]
--progress <MODE> Enable or disable progress bars [default: auto] [possible values: auto,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: tests/test_noseyparker_help.rs
source: crates/noseyparker-cli/tests/help/mod.rs
expression: stdout
---
Nosey Parker is a command-line program that finds secrets and sensitive information in textual data
Expand All @@ -23,6 +23,7 @@ Options:

Global Options:
-v, --verbose... Enable verbose output
-q, --quiet Suppress non-error feedback messages
--color <MODE> Enable or disable colored output [default: auto] [possible values: auto,
never, always]
--progress <MODE> Enable or disable progress bars [default: auto] [possible values: auto,
Expand Down
Loading

0 comments on commit ed0373b

Please sign in to comment.