Skip to content

Commit

Permalink
Simplify ruleset selection
Browse files Browse the repository at this point in the history
- The `np.default` ruleset was renamed to simply `default`

- The `--enable-default-ruleset` CLI option was removed

- The `--ruleset ID` option now disables the default ruleset; to still
  use that, you need to specify an additional `--ruleset default`.
  This seems more ergonomic for the typical use case of alternative
  rulesets, where you want to *not* use the defaults.
  • Loading branch information
bradlarsen committed Nov 28, 2023
1 parent dd4cc92 commit 9db74b5
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 45 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- A new "ruleset" mechanism has been added.
A ruleset is a named collection of rules that can be selected as a group.
The new `--ruleset=NAME` parameter to `scan` can be used to enable additional rulesets.
Three built-in rulesets are provided (`np.default`, `np.assets` and `np.hashes`); the special ruleset name `all` enables all known rules.
The default ruleset can be disabled using the new `--enable-default-ruleset=false` parameter to `scan`.
The new `--ruleset=NAME` parameter to `scan` can be used to enable alternative rulesets.
Three built-in rulesets are provided (`default`, `np.assets` and `np.hashes`); the special ruleset name `all` enables all known rules.
See the built-in rulesets at `crates/noseyparker/data/default/builtin/rulesets` for an example for writing your own.

- The default collection of rules has been pruned down to further emphasize signal-to-noise.
Expand Down
13 changes: 6 additions & 7 deletions crates/noseyparker-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,19 @@ pub struct RuleSpecifierArgs {
#[arg(long, value_name = "PATH", value_hint = ValueHint::AnyPath)]
pub rules: Vec<PathBuf>,

/// Enable an additional ruleset with the specified ID
/// Enable the ruleset with the specified ID
///
/// The ID must resolve to a built-in ruleset or to an additional ruleset loaded with the
/// `--rule-load-path=PATH` option.
/// `--rules=PATH` option.
///
/// The special `all` ID causes all loaded rules to be used.
///
/// This option can be repeated.
#[arg(long, value_name = "ID")]
///
/// Specifying this option disables the default ruleset.
/// If you want to use a custom ruleset in addition to the default ruleset, specify this option twice, e.g., `--ruleset default --ruleset CUSTOM_ID`.
#[arg(long, value_name = "ID", default_values_t=["default".to_string()])]
pub ruleset: Vec<String>,

/// Enable or disable the default ruleset
#[arg(long, default_value_t=true, action=ArgAction::Set, value_name="BOOL")]
pub enable_default_ruleset: bool,
}

/// The mode to use for cloning a Git repository
Expand Down
15 changes: 1 addition & 14 deletions crates/noseyparker-cli/src/rule_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tracing::{debug, info};

use noseyparker::defaults::{get_builtin_rules, DEFAULT_RULESET_ID};
use noseyparker::defaults::get_builtin_rules;
use noseyparker_rules::{Rule, Rules, Ruleset};

use crate::args::RuleSpecifierArgs;
use crate::util::Counted;

pub struct RuleLoader {
load_builtins: bool,
enable_default_ruleset: bool,
additional_load_paths: Vec<PathBuf>,
enabled_ruleset_ids: Vec<String>,
}
Expand All @@ -22,18 +21,11 @@ impl RuleLoader {
pub fn new() -> Self {
Self {
load_builtins: true,
enable_default_ruleset: true,
additional_load_paths: Vec::new(),
enabled_ruleset_ids: Vec::new(),
}
}

/// Enable or disable the default ruleset.
pub fn enable_default_ruleset(mut self, enable_default_ruleset: bool) -> Self {
self.enable_default_ruleset = enable_default_ruleset;
self
}

/// Add additional file or directory paths to load rules and rulesets from.
pub fn additional_rule_load_paths<P: AsRef<Path>, I: IntoIterator<Item = P>>(
mut self,
Expand Down Expand Up @@ -68,10 +60,6 @@ impl RuleLoader {

let mut enabled_ruleset_ids = self.enabled_ruleset_ids.clone();

if self.enable_default_ruleset {
enabled_ruleset_ids.push(DEFAULT_RULESET_ID.to_string());
}

enabled_ruleset_ids.sort();
enabled_ruleset_ids.dedup();

Expand All @@ -97,7 +85,6 @@ impl RuleLoader {
Self::new()
.additional_rule_load_paths(specs.rules.as_slice())
.enable_ruleset_ids(specs.ruleset.iter())
.enable_default_ruleset(specs.enable_default_ruleset)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ Rule Selection Options:
This option can be repeated.

--ruleset <ID>
Enable an additional ruleset with the specified ID
Enable the ruleset with the specified ID

The ID must resolve to a built-in ruleset or to an additional ruleset loaded with the
`--rule-load-path=PATH` option.
`--rules=PATH` option.

The special `all` ID causes all loaded rules to be used.

This option can be repeated.

--enable-default-ruleset <BOOL>
Enable or disable the default ruleset

[default: true]
[possible values: true, false]
Specifying this option disables the default ruleset. If you want to use a custom ruleset
in addition to the default ruleset, specify this option twice, e.g., `--ruleset default
--ruleset CUSTOM_ID`.

[default: default]

Input Specifier Options:
[INPUT]...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ Options:
-h, --help Print help (see more with '--help')

Rule Selection Options:
--rules <PATH> Load additional rules and rulesets from the specified file or
directory
--ruleset <ID> Enable an additional ruleset with the specified ID
--enable-default-ruleset <BOOL> Enable or disable the default ruleset [default: true]
[possible values: true, false]
--rules <PATH> Load additional rules and rulesets from the specified file or directory
--ruleset <ID> Enable the ruleset with the specified ID [default: default]

Input Specifier Options:
[INPUT]... Scan the specified file, directory, or local Git repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,16 @@ expression: stdout
}
],
"rulesets": [
{
"id": "default",
"name": "Nosey Parker default rules",
"num_rules": 94
},
{
"id": "np.assets",
"name": "Nosey Parker asset detection rules",
"num_rules": 15
},
{
"id": "np.default",
"name": "Nosey Parker default rules",
"num_rules": 94
},
{
"id": "np.hashes",
"name": "Nosey Parker password hash rules",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ expression: stdout

Ruleset ID Ruleset Name Rules
─────────────────────────────────────────────────────────
default Nosey Parker default rules 94
np.assets Nosey Parker asset detection rules 15
np.default Nosey Parker default rules 94
np.hashes Nosey Parker password hash rules 5

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rulesets:

- id: np.default
- id: default

name: Nosey Parker default rules

Expand Down
2 changes: 0 additions & 2 deletions crates/noseyparker/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub static DEFAULT_RULES_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/data/d

pub static DEFAULT_IGNORE_RULES: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/default/ignore.conf"));

pub static DEFAULT_RULESET_ID: &str = "np.default";

fn load_yaml_files<'a>(dir: &Dir<'a>) -> Vec<(&'a Path, &'a [u8])> {
dir
.find("**/*.yml")
Expand Down

0 comments on commit 9db74b5

Please sign in to comment.