Skip to content

Commit 44bb510

Browse files
jrnt30databus23
authored andcommitted
Enabling selecting filtering of diff output (#12)
* Enabling selecting filtering of diff output * Adjusting help text based on feedback
1 parent 3801474 commit 44bb510

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

diff.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,45 @@ import (
77

88
"github.com/aryann/difflib"
99
"github.com/mgutz/ansi"
10+
11+
"github.com/databus23/helm-diff/manifest"
1012
)
1113

12-
func diffManifests(oldIndex, newIndex map[string]string, to io.Writer) {
14+
func diffManifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, to io.Writer) {
1315
for key, oldContent := range oldIndex {
1416
if newContent, ok := newIndex[key]; ok {
15-
if oldContent != newContent {
17+
if oldContent.Content != newContent.Content {
1618
// modified
1719
fmt.Fprintf(to, ansi.Color("%s has changed:", "yellow")+"\n", key)
18-
printDiff(oldContent, newContent, to)
20+
printDiff(suppressedKinds, oldContent.Kind, oldContent.Content, newContent.Content, to)
1921
}
2022
} else {
2123
// removed
2224
fmt.Fprintf(to, ansi.Color("%s has been removed:", "yellow")+"\n", key)
23-
printDiff(oldContent, "", to)
25+
printDiff(suppressedKinds, oldContent.Kind, oldContent.Content, "", to)
2426
}
2527
}
2628

2729
for key, newContent := range newIndex {
2830
if _, ok := oldIndex[key]; !ok {
2931
// added
3032
fmt.Fprintf(to, ansi.Color("%s has been added:", "yellow")+"\n", key)
31-
printDiff("", newContent, to)
33+
printDiff(suppressedKinds, newContent.Kind, "", newContent.Content, to)
3234
}
3335
}
3436
}
3537

36-
func printDiff(before, after string, to io.Writer) {
38+
func printDiff(suppressedKinds []string, kind, before, after string, to io.Writer) {
3739
diffs := difflib.Diff(strings.Split(before, "\n"), strings.Split(after, "\n"))
3840

41+
for _, ckind := range suppressedKinds {
42+
if ckind == kind {
43+
str := fmt.Sprintf("+ Changes suppressed on sensitive content of type %s\n", kind)
44+
fmt.Fprintf(to, ansi.Color(str, "yellow"))
45+
return
46+
}
47+
}
48+
3949
for _, diff := range diffs {
4050
text := diff.Payload
4151

@@ -48,5 +58,4 @@ func printDiff(before, after string, to io.Writer) {
4858
fmt.Fprintf(to, "%s\n", " "+text)
4959
}
5060
}
51-
5261
}

main.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ This can be used visualize what changes a helm upgrade will
1919
perform.
2020
`
2121

22-
var Version string = "HEAD"
22+
// Version identifier populated via the CI/CD process.
23+
var Version = "HEAD"
2324

2425
type diffCmd struct {
25-
release string
26-
chart string
27-
// out io.Writer
28-
client helm.Interface
29-
// version int32
30-
valueFiles valueFiles
31-
values []string
32-
reuseValues bool
26+
release string
27+
chart string
28+
client helm.Interface
29+
valueFiles valueFiles
30+
values []string
31+
reuseValues bool
32+
suppressedKinds []string
3333
}
3434

3535
func main() {
36-
3736
diff := diffCmd{}
3837

3938
cmd := &cobra.Command{
@@ -45,10 +44,15 @@ func main() {
4544
fmt.Println(Version)
4645
return nil
4746
}
47+
4848
if err := checkArgsLength(len(args), "release name", "chart path"); err != nil {
4949
return err
5050
}
5151

52+
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
53+
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
54+
}
55+
5256
diff.release = args[0]
5357
diff.chart = args[1]
5458
if diff.client == nil {
@@ -60,9 +64,11 @@ func main() {
6064

6165
f := cmd.Flags()
6266
f.BoolP("version", "v", false, "show version")
67+
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
6368
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
6469
f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
6570
f.BoolVar(&diff.reuseValues, "reuse-values", false, "reuse the last release's values and merge in any new values")
71+
f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
6672

6773
if err := cmd.Execute(); err != nil {
6874
os.Exit(1)
@@ -100,7 +106,7 @@ func (d *diffCmd) run() error {
100106
currentSpecs := manifest.Parse(releaseResponse.Release.Manifest)
101107
newSpecs := manifest.Parse(upgradeResponse.Release.Manifest)
102108

103-
diffManifests(currentSpecs, newSpecs, os.Stdout)
109+
diffManifests(currentSpecs, newSpecs, d.suppressedKinds, os.Stdout)
104110

105111
return nil
106112
}

manifest/parse.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212

1313
var yamlSeperator = []byte("\n---\n")
1414

15+
type MappingResult struct {
16+
Name string
17+
Kind string
18+
Content string
19+
}
20+
1521
type metadata struct {
1622
ApiVersion string `yaml:"apiVersion"`
1723
Kind string
@@ -47,15 +53,15 @@ func splitSpec(token string) (string, string) {
4753
return "", ""
4854
}
4955

50-
func Parse(manifest string) map[string]string {
56+
func Parse(manifest string) map[string]*MappingResult {
5157
scanner := bufio.NewScanner(strings.NewReader(manifest))
5258
scanner.Split(scanYamlSpecs)
5359
//Allow for tokens (specs) up to 1M in size
5460
scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), 1048576)
5561
//Discard the first result, we only care about everything after the first seperator
5662
scanner.Scan()
5763

58-
result := make(map[string]string)
64+
result := make(map[string]*MappingResult)
5965

6066
for scanner.Scan() {
6167
content := scanner.Text()
@@ -70,7 +76,11 @@ func Parse(manifest string) map[string]string {
7076
if _, ok := result[name]; ok {
7177
log.Println("Error: Found duplicate key %#v in manifest", name)
7278
} else {
73-
result[name] = content
79+
result[name] = &MappingResult{
80+
Name: name,
81+
Kind: metadata.Kind,
82+
Content: content,
83+
}
7484
}
7585
}
7686
return result

0 commit comments

Comments
 (0)