diff --git a/config/config.go b/config/config.go index 7cf5a3e..60dba08 100644 --- a/config/config.go +++ b/config/config.go @@ -55,12 +55,13 @@ type FileRule struct { } type GrepRule struct { - Path string `validate:"required"` - Pattern string `validate:"required"` - Include string - Recursive bool - Match bool - SkipNotFound bool `yaml:"skip-not-found"` + Path string `validate:"required"` + Pattern string `validate:"required"` + Include string + ExtendedRegexp bool `yaml:"extended-regexp"` + Recursive bool + Match bool + SkipNotFound bool `yaml:"skip-not-found"` } type ProjectRule struct { diff --git a/doc/rules/grep.md b/doc/rules/grep.md index 64d736b..a8c4872 100644 --- a/doc/rules/grep.md +++ b/doc/rules/grep.md @@ -19,5 +19,6 @@ Available options are: - `path`: the file or directory to check (mandatory) - `pattern`: the string pattern to search for (mandatory) -- `recursive`: recursively search subdirectories listed (grep `-r`option) -- `match`: if set to true, the module will be successful if the pattern is found. Default to false, where the module will be successful if the pattern is **not** found. +- `recursive`: recursively search subdirectories listed (grep `-r` option) +- `match`: if set to true, the module will be successful if the pattern is found. Default to false, where the module will be successful if the pattern is **not** found +- `extended-regexp`: if set to true, the pattern is treated as an extended regular expression (grep `-E` option) diff --git a/pkg/ruler/rules/grep.go b/pkg/ruler/rules/grep.go index 7ec34c9..95a678c 100644 --- a/pkg/ruler/rules/grep.go +++ b/pkg/ruler/rules/grep.go @@ -14,22 +14,24 @@ import ( ) type GrepRule struct { - Path string - Include string - Pattern string - Recursive bool - Match bool - SkipNotFound bool + Path string + Include string + Pattern string + ExtendedRegexp bool + Recursive bool + Match bool + SkipNotFound bool } func NewGrepRule(config config.GrepRule) *GrepRule { return &GrepRule{ - Path: config.Path, - Recursive: config.Recursive, - Pattern: config.Pattern, - Include: config.Include, - Match: config.Match, - SkipNotFound: config.SkipNotFound, + Path: config.Path, + Recursive: config.Recursive, + Pattern: config.Pattern, + ExtendedRegexp: config.ExtendedRegexp, + Include: config.Include, + Match: config.Match, + SkipNotFound: config.SkipNotFound, } } @@ -47,6 +49,9 @@ func (rule *GrepRule) Do(ctx context.Context, project project.Project) error { if rule.Include != "" { arguments = append(arguments, "--include", rule.Include) } + if rule.ExtendedRegexp { + arguments = append(arguments, "--extended-regexp") + } arguments = append(arguments, rule.Pattern, path) cmd := exec.CommandContext(ctx, "grep", arguments...) //nolint diff --git a/pkg/ruler/rules/grep_test.go b/pkg/ruler/rules/grep_test.go index 4b14b9c..7c91ca8 100644 --- a/pkg/ruler/rules/grep_test.go +++ b/pkg/ruler/rules/grep_test.go @@ -81,6 +81,27 @@ func TestGrepRule(t *testing.T) { Match: true, }, }, + { + rule: rules.GrepRule{ + Path: "_testdata", + Recursive: true, + Include: "file1", + Pattern: "abc?defg", + ExtendedRegexp: false, + Match: true, + }, + error: "no match for pattern", + }, + { + rule: rules.GrepRule{ + Path: "_testdata", + Recursive: true, + Include: "file1", + Pattern: "abc?defg", + ExtendedRegexp: true, + Match: true, + }, + }, { rule: rules.GrepRule{ Path: "_testdata",