Skip to content

Commit c6c1502

Browse files
committed
Suppress diff output by regex
Signed-off-by: Jan-Otto Kröpke <[email protected]>
1 parent 882b211 commit c6c1502

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

cmd/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
1515
f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template, dyff. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
1616
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
1717
f.Float32VarP(&o.FindRenames, "find-renames", "D", 0, "Enable rename detection if set to any value greater than 0. If specified, the value denotes the maximum fraction of changed content as lines added + removed compared to total lines in a diff for considering it a rename. Only objects of the same Kind are attempted to be matched")
18+
f.StringArrayVar(&o.SuppressedOutputLineRegex, "suppress-output-line-regex", []string{}, "a regex to suppress diff output lines that match")
1819
}
1920

2021
// ProcessDiffOptions processes the set flags and handles possible interactions between them

diff/diff.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"math"
8+
"regexp"
89
"sort"
910
"strings"
1011

@@ -20,12 +21,13 @@ import (
2021

2122
// Options are all the options to be passed to generate a diff
2223
type Options struct {
23-
OutputFormat string
24-
OutputContext int
25-
StripTrailingCR bool
26-
ShowSecrets bool
27-
SuppressedKinds []string
28-
FindRenames float32
24+
OutputFormat string
25+
OutputContext int
26+
StripTrailingCR bool
27+
ShowSecrets bool
28+
SuppressedKinds []string
29+
FindRenames float32
30+
SuppressedOutputLineRegex []string
2931
}
3032

3133
// Manifests diff on manifests
@@ -64,8 +66,55 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O
6466
doDiff(&report, key, nil, newContent, options)
6567
}
6668

69+
filteredReport := Report{}
70+
if len(options.SuppressedOutputLineRegex) > 0 {
71+
filteredReport.format = report.format
72+
filteredReport.entries = []ReportEntry{}
73+
74+
var suppressOutputRegexes []*regexp.Regexp
75+
76+
for _, suppressOutputRegex := range options.SuppressedOutputLineRegex {
77+
regexp, err := regexp.Compile(suppressOutputRegex)
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
suppressOutputRegexes = append(suppressOutputRegexes, regexp)
83+
}
84+
85+
for _, entry := range report.entries {
86+
var diffs []difflib.DiffRecord
87+
88+
for _, diff := range entry.diffs {
89+
matched := false
90+
91+
for _, suppressOutputRegex := range suppressOutputRegexes {
92+
if suppressOutputRegex.MatchString(diff.Payload) {
93+
matched = true
94+
break
95+
}
96+
}
97+
98+
if !matched {
99+
diffs = append(diffs, diff)
100+
}
101+
}
102+
103+
// Add entry to the report, if diffs are present.
104+
for _, diff := range diffs {
105+
if diff.Delta.String() != " " {
106+
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType)
107+
break
108+
}
109+
}
110+
}
111+
} else {
112+
filteredReport = report
113+
}
114+
67115
seenAnyChanges := len(report.entries) > 0
68-
report.print(to)
116+
filteredReport.print(to)
117+
filteredReport.clean()
69118
report.clean()
70119
return seenAnyChanges
71120
}

diff/diff_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ spec:
260260

261261
t.Run("OnChange", func(t *testing.T) {
262262
var buf1 bytes.Buffer
263-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0}
263+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}
264264

265265
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
266266
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -279,7 +279,7 @@ spec:
279279

280280
t.Run("OnChangeRename", func(t *testing.T) {
281281
var buf1 bytes.Buffer
282-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5}
282+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}
283283

284284
if changesSeen := Manifests(specReleaseSpec, specReleaseRenamed, &diffOptions, &buf1); !changesSeen {
285285
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -300,7 +300,7 @@ spec:
300300

301301
t.Run("OnChangeRenameAndUpdate", func(t *testing.T) {
302302
var buf1 bytes.Buffer
303-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5}
303+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}
304304

305305
if changesSeen := Manifests(specReleaseSpec, specReleaseRenamedAndUpdated, &diffOptions, &buf1); !changesSeen {
306306
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -322,7 +322,7 @@ spec:
322322

323323
t.Run("OnChangeRenameAndAdded", func(t *testing.T) {
324324
var buf1 bytes.Buffer
325-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5}
325+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}
326326

327327
if changesSeen := Manifests(specReleaseSpec, specReleaseRenamedAndAdded, &diffOptions, &buf1); !changesSeen {
328328
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -346,7 +346,7 @@ spec:
346346

347347
t.Run("OnChangeRenameAndRemoved", func(t *testing.T) {
348348
var buf1 bytes.Buffer
349-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5}
349+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}
350350

351351
if changesSeen := Manifests(specReleaseRenamedAndAdded, specReleaseSpec, &diffOptions, &buf1); !changesSeen {
352352
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -370,7 +370,7 @@ spec:
370370

371371
t.Run("OnNoChange", func(t *testing.T) {
372372
var buf2 bytes.Buffer
373-
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0}
373+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}
374374

375375
if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen {
376376
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
@@ -381,7 +381,7 @@ spec:
381381

382382
t.Run("OnChangeSimple", func(t *testing.T) {
383383
var buf1 bytes.Buffer
384-
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0}
384+
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}}
385385

386386
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
387387
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -394,7 +394,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
394394

395395
t.Run("OnNoChangeSimple", func(t *testing.T) {
396396
var buf2 bytes.Buffer
397-
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0}
397+
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}}
398398
if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen {
399399
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
400400
}
@@ -404,7 +404,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
404404

405405
t.Run("OnChangeTemplate", func(t *testing.T) {
406406
var buf1 bytes.Buffer
407-
diffOptions := Options{"template", 10, false, true, []string{}, 0.0}
407+
diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}}
408408

409409
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
410410
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -422,7 +422,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
422422

423423
t.Run("OnChangeJSON", func(t *testing.T) {
424424
var buf1 bytes.Buffer
425-
diffOptions := Options{"json", 10, false, true, []string{}, 0.0}
425+
diffOptions := Options{"json", 10, false, true, []string{}, 0.0, []string{}}
426426

427427
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
428428
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -440,7 +440,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
440440

441441
t.Run("OnNoChangeTemplate", func(t *testing.T) {
442442
var buf2 bytes.Buffer
443-
diffOptions := Options{"template", 10, false, true, []string{}, 0.0}
443+
diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}}
444444

445445
if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen {
446446
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
@@ -452,7 +452,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
452452
t.Run("OnChangeCustomTemplate", func(t *testing.T) {
453453
var buf1 bytes.Buffer
454454
os.Setenv("HELM_DIFF_TPL", "testdata/customTemplate.tpl")
455-
diffOptions := Options{"template", 10, false, true, []string{}, 0.0}
455+
diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}}
456456

457457
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
458458
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
@@ -535,7 +535,7 @@ stringData:
535535

536536
t.Run("OnChangeSecretWithByteData", func(t *testing.T) {
537537
var buf1 bytes.Buffer
538-
diffOptions := Options{"diff", 10, false, false, []string{}, 0.5} //NOTE: ShowSecrets = false
538+
diffOptions := Options{"diff", 10, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
539539

540540
if changesSeen := Manifests(specSecretWithByteData, specSecretWithByteDataChanged, &diffOptions, &buf1); !changesSeen {
541541
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -560,7 +560,7 @@ stringData:
560560

561561
t.Run("OnChangeSecretWithStringData", func(t *testing.T) {
562562
var buf1 bytes.Buffer
563-
diffOptions := Options{"diff", 10, false, false, []string{}, 0.5} //NOTE: ShowSecrets = false
563+
diffOptions := Options{"diff", 10, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
564564

565565
if changesSeen := Manifests(specSecretWithStringData, specSecretWithStringDataChanged, &diffOptions, &buf1); !changesSeen {
566566
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")

0 commit comments

Comments
 (0)