Skip to content

Commit

Permalink
feat: add null-data grep flag (-z) to config options: (#80)
Browse files Browse the repository at this point in the history
- should facilitate regex multi-lines matching with grep (on yaml files for example)

Signed-off-by: ValentinLvr <valentin.levier@outlook.com>
Signed-off-by: Stefano Celentano <stefano.celentano@qonto.com>
  • Loading branch information
ValentinLvr authored and Stefano Celentano committed Aug 14, 2024
1 parent 3efe291 commit ece14b0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type GrepRule struct {
Recursive bool
Match bool
SkipNotFound bool `yaml:"skip-not-found"`
NullData bool `yaml:"null-data"`
}

type ProjectRule struct {
Expand Down
1 change: 1 addition & 0 deletions doc/rules/grep.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Available options are:
- `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)
- `null-data`: if set to true, treat input data as sequences of lines terminated by a zero-byte instead of a newline. Useful with multi-line matching. (grep `-z` option)
3 changes: 2 additions & 1 deletion pkg/ruler/rules/_testdata/file1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
abcdefg
<3_ruby
azerty
azerty
<3_go
5 changes: 5 additions & 0 deletions pkg/ruler/rules/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type GrepRule struct {
Recursive bool
Match bool
SkipNotFound bool
NullData bool
}

func NewGrepRule(config config.GrepRule) *GrepRule {
Expand All @@ -32,6 +33,7 @@ func NewGrepRule(config config.GrepRule) *GrepRule {
Include: config.Include,
Match: config.Match,
SkipNotFound: config.SkipNotFound,
NullData: config.NullData,
}
}

Expand All @@ -52,6 +54,9 @@ func (rule *GrepRule) Do(ctx context.Context, project project.Project) error {
if rule.ExtendedRegexp {
arguments = append(arguments, "--extended-regexp")
}
if rule.NullData {
arguments = append(arguments, "-z")
}
arguments = append(arguments, rule.Pattern, path)

cmd := exec.CommandContext(ctx, "grep", arguments...) //nolint
Expand Down
23 changes: 23 additions & 0 deletions pkg/ruler/rules/grep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ func TestGrepRule(t *testing.T) {
},
error: "match found for pattern",
},
{
rule: rules.GrepRule{
Path: "_testdata",
Recursive: true,
Include: "file1",
Pattern: "azerty.*[[:space:]]+<3_go",
ExtendedRegexp: true,
Match: true,
NullData: false,
},
error: "no match for pattern",
},
{
rule: rules.GrepRule{
Path: "_testdata",
Recursive: true,
Include: "file1",
Pattern: "azerty.*[[:space:]]+<3_go",
ExtendedRegexp: true,
Match: true,
NullData: true,
},
},
}
for _, c := range cases {
err := c.rule.Do(context.Background(), project)
Expand Down

0 comments on commit ece14b0

Please sign in to comment.