Skip to content

Commit 7edb114

Browse files
fwieselmumoshu
authored andcommitted
Extract redaction/diff/report generation
The three steps are called in three places, we better combine them into a single function.
1 parent e8b47f4 commit 7edb114

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

diff/diff.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,67 @@ import (
2222
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, stripTrailingCR bool, to io.Writer) bool {
2323
report := Report{}
2424
report.setupReportFormat(output)
25-
seenAnyChanges := false
26-
emptyMapping := &manifest.MappingResult{}
2725
for _, key := range sortedKeys(oldIndex) {
2826
oldContent := oldIndex[key]
2927

3028
if newContent, ok := newIndex[key]; ok {
31-
if oldContent.Content != newContent.Content {
32-
// modified
33-
if !showSecrets {
34-
redactSecrets(oldContent, newContent)
35-
}
36-
37-
diffs := diffMappingResults(oldContent, newContent, stripTrailingCR)
38-
if len(diffs) > 0 {
39-
seenAnyChanges = true
40-
}
41-
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "MODIFY")
42-
}
29+
// modified?
30+
doDiff(key, oldContent, newContent, suppressedKinds, showSecrets, stripTrailingCR, context)
4331
} else {
4432
// removed
45-
if !showSecrets {
46-
redactSecrets(oldContent, nil)
47-
48-
}
49-
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
50-
if len(diffs) > 0 {
51-
seenAnyChanges = true
52-
}
53-
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "REMOVE")
33+
doDiff(key, oldContent, nil, suppressedKinds, showSecrets, stripTrailingCR, context)
5434
}
5535
}
5636

5737
for _, key := range sortedKeys(newIndex) {
5838
newContent := newIndex[key]
5939

6040
if _, ok := oldIndex[key]; !ok {
61-
// added
62-
if !showSecrets {
63-
redactSecrets(nil, newContent)
64-
}
65-
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
66-
if len(diffs) > 0 {
67-
seenAnyChanges = true
68-
}
69-
report.addEntry(key, suppressedKinds, newContent.Kind, context, diffs, "ADD")
41+
doDiff(key, nil, newContent, suppressedKinds, showSecrets, stripTrailingCR, context)
7042
}
7143
}
44+
45+
seenAnyChanges := len(report.entries) > 0
7246
report.print(to)
7347
report.clean()
7448
return seenAnyChanges
7549
}
7650

51+
func actualChanges(diff []difflib.DiffRecord) int {
52+
changes := 0
53+
for _, record := range diff {
54+
if record.Delta != difflib.Common {
55+
changes++
56+
}
57+
}
58+
return changes
59+
}
60+
61+
func doDiff(key string, oldContent *manifest.MappingResult, newContent *manifest.MappingResult, suppressedKinds []string, showSecrets bool, stripTrailingCR bool, context int) {
62+
if oldContent != nil && newContent != nil && oldContent.Content == newContent.Content {
63+
return
64+
}
65+
66+
if !showSecrets {
67+
redactSecrets(oldContent, newContent)
68+
}
69+
70+
if oldContent == nil {
71+
emptyMapping := &manifest.MappingResult{}
72+
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
73+
report.addEntry(key, suppressedKinds, newContent.Kind, context, diffs, "ADD")
74+
} else if newContent == nil {
75+
emptyMapping := &manifest.MappingResult{}
76+
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
77+
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "REMOVE")
78+
} else {
79+
diffs := diffMappingResults(oldContent, newContent, stripTrailingCR)
80+
if actualChanges(diffs) > 0 {
81+
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "MODIFY")
82+
}
83+
}
84+
}
85+
7786
func redactSecrets(old, new *manifest.MappingResult) {
7887
if (old != nil && old.Kind != "Secret") || (new != nil && new.Kind != "Secret") {
7988
return

0 commit comments

Comments
 (0)