Skip to content

Commit

Permalink
Merge pull request #77 from qonto/mdemolin-extended-regex
Browse files Browse the repository at this point in the history
Add support for extended regexp in grep rule
  • Loading branch information
MaximeD authored Jul 4, 2024
2 parents 2b35bc2 + bd326c7 commit 7edc5d2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
13 changes: 7 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions doc/rules/grep.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 17 additions & 12 deletions pkg/ruler/rules/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/ruler/rules/grep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7edc5d2

Please sign in to comment.