Skip to content

Commit

Permalink
Add ability to disable default rules
Browse files Browse the repository at this point in the history
if you add the following to a config file and use that to run woke, you can disable a default rule that matches the name

```yaml
rules:
  - name: whitelist
```
  • Loading branch information
caitlinelfring committed Sep 3, 2020
1 parent e2fc97e commit 94f1ddb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ type Rule struct {
}

func (r *Rule) FindAllStringIndex(text string) [][]int {
// If no terms are provided, this essentially disables the rule
// which is helpful for disabling default rules. Eventually, there should be
// a way to disable a default rule, and then, if a rule has no Terms, it falls back to the Name.
if len(r.Terms) == 0 {
return [][]int{}
}

if r.re == nil {
r.re = regexp.MustCompile(fmt.Sprintf(`(?i)\b(%s)\b`, strings.Join(r.Terms, "|")))
}
return r.re.FindAllStringIndex(text, -1)
}

func (r *Rule) String() string {
return r.Name
return r.re.FindAllStringIndex(text, -1)
}

// Reason returns a human-readable reason for the rule violation
Expand Down
3 changes: 3 additions & 0 deletions pkg/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestRule_FindAllStringIndex(t *testing.T) {
got := r.FindAllStringIndex(test.text)
assert.Equal(t, test.expected, got)
}

e := Rule{Name: "rule1"}
assert.Equal(t, [][]int{}, e.FindAllStringIndex("rule1"))
}

func TestRule_Reason(t *testing.T) {
Expand Down

0 comments on commit 94f1ddb

Please sign in to comment.