From 94f1ddbe64fe69e761424ab3af906e9af7bb0cbb Mon Sep 17 00:00:00 2001 From: Caitlin Elfring Date: Thu, 3 Sep 2020 02:30:43 -0400 Subject: [PATCH] Add ability to disable default rules 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 ``` --- pkg/rule/rule.go | 12 ++++++++---- pkg/rule/rule_test.go | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/rule/rule.go b/pkg/rule/rule.go index 1f1fe4e4..20a4c103 100644 --- a/pkg/rule/rule.go +++ b/pkg/rule/rule.go @@ -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 diff --git a/pkg/rule/rule_test.go b/pkg/rule/rule_test.go index a95cefd3..b82f4ab3 100644 --- a/pkg/rule/rule_test.go +++ b/pkg/rule/rule_test.go @@ -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) {