Skip to content

Commit

Permalink
Add exclude option and support filter option
Browse files Browse the repository at this point in the history
Add exclude option & support regular expression on filter option

Split options to filter or exclude by regular expression
  • Loading branch information
sachaos authored and HeavyWombat committed May 11, 2022
1 parent 416d58b commit 4cf6fb5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
22 changes: 12 additions & 10 deletions internal/cmd/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@ types are: YAML (http://yaml.org/) and JSON (http://json.org/).
}

if reportOptions.filters != nil {
var filterPaths []*ytbx.Path
for _, pathString := range reportOptions.filters {
path, err := ytbx.ParsePathStringUnsafe(pathString)
if err != nil {
return wrap.Errorf(err, "failed to set path filter, because path %s cannot be parsed", pathString)
}

filterPaths = append(filterPaths, &path)
}
report = report.Filter(reportOptions.filters...)
}

if reportOptions.filterRegexps != nil {
report = report.FilterRegexp(reportOptions.filterRegexps...)
}

if reportOptions.excludes != nil {
report = report.Exclude(reportOptions.excludes...)
}

report = report.Filter(filterPaths...)
if reportOptions.excludeRegexps != nil {
report = report.ExcludeRegexp(reportOptions.excludeRegexps...)
}

return writeReport(cmd, report)
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type reportConfig struct {
omitHeader bool
useGoPatchPaths bool
filters []string
excludes []string
filterRegexps []string
excludeRegexps []string
}

var reportOptions reportConfig
Expand All @@ -60,6 +63,9 @@ func applyReportOptionsFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&reportOptions.ignoreOrderChanges, "ignore-order-changes", "i", false, "ignore order changes in lists")
cmd.Flags().BoolVarP(&reportOptions.kubernetesEntityDetection, "detect-kubernetes", "", true, "detect kubernetes entities")
cmd.Flags().StringSliceVar(&reportOptions.filters, "filter", nil, "filter reports to a subset of differences based on supplied arguments")
cmd.Flags().StringSliceVar(&reportOptions.excludes, "exclude", nil, "exclude reports from a set of differences based on supplied arguments")
cmd.Flags().StringSliceVar(&reportOptions.filterRegexps, "filter-regexp", nil, "filter reports to a subset of differences based on supplied regular expressions")
cmd.Flags().StringSliceVar(&reportOptions.excludeRegexps, "exclude-regexp", nil, "exclude reports from a set of differences based on supplied regular expressions")

// Main output preferences
cmd.Flags().StringVarP(&reportOptions.style, "output", "o", defaultOutputStyle, "specify the output style, supported styles: human, or brief")
Expand Down
37 changes: 34 additions & 3 deletions pkg/dyff/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,19 +735,50 @@ listY: [ Yo, Yo, Yo ]

It("should filter my report based on set of paths", func() {
pathString := "/yaml/map/foobar"
p := path(pathString)

report := Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
singleDiff("/yaml/map/barfoo", ADDITION, nil, "barfoo"),
}}

Expect(report.Filter()).To(BeEquivalentTo(report))
Expect(report.Filter(p)).To(BeEquivalentTo(Report{Diffs: []Diff{
Expect(report.Filter(pathString)).To(BeEquivalentTo(Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
}}))

Expect(report.Filter(path("/does/not/exist"))).To(BeEquivalentTo(Report{}))
Expect(report.Filter("/does/not/exist")).To(BeEquivalentTo(Report{}))
})

It("should filter my report based on set of regular expressions", func() {
pathString := "/yaml/map/foobar"

report := Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
singleDiff("/yaml/map/barfoo", ADDITION, nil, "barfoo"),
}}

Expect(report.FilterRegexp()).To(BeEquivalentTo(report))
Expect(report.FilterRegexp("foobar")).To(BeEquivalentTo(Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
}}))

Expect(report.FilterRegexp("/does/not/exist")).To(BeEquivalentTo(Report{}))
})

It("should exclude my report based on regular expressions", func() {
pathString := "/yaml/map/foobar"

report := Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
singleDiff("/yaml/map/barfoo", ADDITION, nil, "barfoo"),
}}

Expect(report.ExcludeRegexp()).To(BeEquivalentTo(report))
Expect(report.ExcludeRegexp("barfoo")).To(BeEquivalentTo(Report{Diffs: []Diff{
singleDiff(pathString, ADDITION, nil, "foobar"),
}}))

Expect(report.ExcludeRegexp("/does/not/exist")).To(BeEquivalentTo(report))
})
})

Expand Down
89 changes: 76 additions & 13 deletions pkg/dyff/reports.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,93 @@
package dyff

import "github.com/gonvenience/ytbx"
import "regexp"

func (r Report) filter(f func(string) bool) (result Report) {
result = Report{
From: r.From,
To: r.To,
}

for _, diff := range r.Diffs {
diffPathString := diff.Path.String()
if f(diffPathString) {
result.Diffs = append(result.Diffs, diff)
}
}

return result
}

// Filter accepts YAML paths as input and returns a new report with differences for those paths only
func (r Report) Filter(paths ...*ytbx.Path) (result Report) {
func (r Report) Filter(paths ...string) (result Report) {
if len(paths) == 0 {
return r
}

result = Report{
From: r.From,
To: r.To,
return r.filter(func(s string) bool {
for _, path := range paths {
if path == s {
return true
}
}
return false
})
}

// Exclude accepts YAML paths as input and returns a new report with differences without those paths
func (r Report) Exclude(paths ...string) (result Report) {
if len(paths) == 0 {
return r
}

pathsMap := make(map[string]struct{})
return r.filter(func(s string) bool {
for _, path := range paths {
if path == s {
return false
}
}
return true
})
}

for _, path := range paths {
pathsMap[path.String()] = struct{}{}
// FilterRegexp accepts regular expressions as input and returns a new report with differences for matching those patterns
func (r Report) FilterRegexp(pattern ...string) (result Report) {
if len(pattern) == 0 {
return r
}

for _, diff := range r.Diffs {
diffPathString := diff.Path.String()
if _, ok := pathsMap[diffPathString]; ok {
result.Diffs = append(result.Diffs, diff)
regexps := make([]*regexp.Regexp, len(pattern))
for i := range pattern {
regexps[i] = regexp.MustCompile(pattern[i])
}

return r.filter(func(s string) bool {
for _, regexp := range regexps {
if regexp.MatchString(s) {
return true
}
}
return false
})
}

// ExcludeRegexp accepts regular expressions as input and returns a new report with differences for not matching those patterns
func (r Report) ExcludeRegexp(pattern ...string) (result Report) {
if len(pattern) == 0 {
return r
}

return result
regexps := make([]*regexp.Regexp, len(pattern))
for i := range pattern {
regexps[i] = regexp.MustCompile(pattern[i])
}

return r.filter(func(s string) bool {
for _, regexp := range regexps {
if regexp.MatchString(s) {
return false
}
}
return true
})
}

0 comments on commit 4cf6fb5

Please sign in to comment.