Skip to content

Commit

Permalink
Add a new --max-matches=N parameter to report
Browse files Browse the repository at this point in the history
Fixes #75.
  • Loading branch information
bradlarsen committed Sep 11, 2023
1 parent 553de0e commit 613629c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Additions

- A default value (`datastore.np`) is now set for commands that take a datastore option ([#74](https://github.com/praetorian-inc/noseyparker/issues/74)).
- A default value (`datastore.np`) is now set for commands that take a datastore parameter ([#74](https://github.com/praetorian-inc/noseyparker/issues/74)).
This makes simpler `noseyparker` command-line invocations possible.

- A new `shell-completions` command has been added, which generates shell-specific completion scripts for zsh, bash, fish, powershell, and elvish ([#76](https://github.com/praetorian-inc/noseyparker/pull/76)).
These generated completion scripts make discovery of Nosey Parker's command-line API simpler.
Thank you @Coruscant11!

- The `report` command supports a new `--max-matches=N` parameter to control the maximum number of matches that will be output for any single finding ([#75](https://github.com/praetorian-inc/noseyparker/issues/75)).
A negative number means "no limit".


### Changes

- All the output formats for the `report` command now respect the new `--max-matches=N` parameter.
Previously, the output formats other than `human` would run without limit (i.e., as though `--max-matches=-1` had been specified).


## [v0.14.0](https://github.com/praetorian-inc/noseyparker/releases/v0.14.0) (2023-08-17)

Expand Down
6 changes: 6 additions & 0 deletions crates/noseyparker-cli/src/bin/noseyparker/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ pub struct ReportArgs {

#[command(flatten)]
pub output_args: OutputArgs<ReportOutputFormat>,

/// Limit the number of matches per finding to at most N
///
/// A negative value means "no limit".
#[arg(long, default_value_t = 3, value_name = "N")]
pub max_matches: i64,
}


Expand Down
34 changes: 21 additions & 13 deletions crates/noseyparker-cli/src/bin/noseyparker/cmd_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@ pub fn run(_global_args: &GlobalArgs, args: &ReportArgs) -> Result<()> {
.output_args
.get_writer()
.context("Failed to get output writer")?;
DetailsReporter(datastore).report(args.output_args.format, output)
let max_matches = if args.max_matches <= 0 {
None
} else {
Some(args.max_matches.try_into().unwrap())
};
let reporter = DetailsReporter { datastore, max_matches };
reporter.report(args.output_args.format, output)
}

struct DetailsReporter(Datastore);
struct DetailsReporter {
datastore: Datastore,
max_matches: Option<usize>,
}

impl DetailsReporter {
fn get_matches(
&self,
metadata: &MatchGroupMetadata,
limit: Option<usize>,
) -> Result<Vec<ReportMatch>> {
Ok(self
.0
.get_match_group_data(metadata, limit)
.datastore
.get_match_group_data(metadata, self.max_matches)
.with_context(|| format!("Failed to get match data for group {metadata:?}"))?
.into_iter()
.map(|(p, md, m)| ReportMatch { ps: p, md, m })
Expand All @@ -60,15 +68,15 @@ impl Reportable for DetailsReporter {

impl DetailsReporter {
fn human_format<W: std::io::Write>(&self, mut writer: W) -> Result<()> {
let datastore = &self.0;
let datastore = &self.datastore;
let group_metadata = datastore
.get_match_group_metadata()
.context("Failed to get match group metadata from datastore")?;

let num_findings = group_metadata.len();
for (finding_num, metadata) in group_metadata.into_iter().enumerate() {
let finding_num = finding_num + 1;
let matches = self.get_matches(&metadata, Some(3))?;
let matches = self.get_matches(&metadata)?;
let match_group = MatchGroup { metadata, matches };
writeln!(
&mut writer,
Expand All @@ -81,7 +89,7 @@ impl DetailsReporter {
}

fn json_format<W: std::io::Write>(&self, writer: W) -> Result<()> {
let datastore = &self.0;
let datastore = &self.datastore;
let group_metadata = datastore
.get_match_group_metadata()
.context("Failed to get match group metadata from datastore")?;
Expand All @@ -90,7 +98,7 @@ impl DetailsReporter {
let es = group_metadata
.into_iter()
.map(|metadata| {
let matches = self.get_matches(&metadata, None)?;
let matches = self.get_matches(&metadata)?;
Ok(MatchGroup { metadata, matches })
})
.collect::<Result<Vec<MatchGroup>, anyhow::Error>>()?;
Expand All @@ -99,13 +107,13 @@ impl DetailsReporter {
}

fn jsonl_format<W: std::io::Write>(&self, mut writer: W) -> Result<()> {
let datastore = &self.0;
let datastore = &self.datastore;
let group_metadata = datastore
.get_match_group_metadata()
.context("Failed to get match group metadata from datastore")?;

for metadata in group_metadata.into_iter() {
let matches = self.get_matches(&metadata, None)?;
let matches = self.get_matches(&metadata)?;
let match_group = MatchGroup { metadata, matches };

serde_json::to_writer(&mut writer, &match_group)?;
Expand All @@ -115,7 +123,7 @@ impl DetailsReporter {
}

fn sarif_format<W: std::io::Write>(&self, mut writer: W) -> Result<()> {
let datastore: &Datastore = &self.0;
let datastore: &Datastore = &self.datastore;
let group_metadata = datastore
.get_match_group_metadata()
.context("Failed to get match group metadata from datastore")?;
Expand All @@ -124,7 +132,7 @@ impl DetailsReporter {
let results: Vec<sarif::Result> = group_metadata
.into_iter()
.map(|metadata| {
let matches = self.get_matches(&metadata, None)?;
let matches = self.get_matches(&metadata)?;

let first_match_blob_id = match matches.first() {
Some(entry) => entry.m.blob_id.to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Output Options:
- jsonl: JSON Lines format
- sarif: SARIF format

--max-matches <N>
Limit the number of matches per finding to at most N

A negative value means "no limit".

[default: 3]

Global Options:
-v, --verbose...
Enable verbose output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Output Options:
-o, --output <PATH> Write output to the specified path
-f, --format <FORMAT> Write output in the specified format [default: human] [possible values:
human, json, jsonl, sarif]
--max-matches <N> Limit the number of matches per finding to at most N [default: 3]

Global Options:
-v, --verbose... Enable verbose output
Expand Down

0 comments on commit 613629c

Please sign in to comment.