Skip to content

Commit d70a28c

Browse files
fwieselmumoshu
authored andcommitted
Extract diff options to struct and add common code
By encapsulating the parameters into a struct and a corresponding cmd.options package helper functions, we need only to change the code in two places when adding a new parameter instead of four (for each subcommand).
1 parent 7edb114 commit d70a28c

File tree

7 files changed

+86
-126
lines changed

7 files changed

+86
-126
lines changed

cmd/options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"github.com/databus23/helm-diff/v3/diff"
5+
"github.com/spf13/pflag"
6+
)
7+
8+
func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
9+
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
10+
f.BoolVar(&o.ShowSecrets, "show-secrets", false, "do not redact secret values in the output")
11+
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
12+
f.IntVarP(&o.OutputContext, "context", "C", -1, "output NUM lines of context around changes")
13+
f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
14+
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
15+
}
16+
17+
func ProcessDiffOptions(f *pflag.FlagSet, o *diff.Options) {
18+
if q, _ := f.GetBool("suppress-secrets"); q {
19+
o.SuppressedKinds = append(o.SuppressedKinds, "Secret")
20+
}
21+
}

cmd/release.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ import (
1515
type release struct {
1616
client helm.Interface
1717
detailedExitCode bool
18-
suppressedKinds []string
1918
releases []string
20-
outputContext int
2119
includeTests bool
22-
showSecrets bool
23-
output string
24-
stripTrailingCR bool
2520
normalizeManifests bool
21+
diff.Options
2622
}
2723

2824
const releaseCmdLongUsage = `
@@ -59,9 +55,7 @@ func releaseCmd() *cobra.Command {
5955
return errors.New("Too few arguments to Command \"release\".\nMinimum 2 arguments required: release name-1, release name-2")
6056
}
6157

62-
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
63-
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
64-
}
58+
ProcessDiffOptions(cmd.Flags(), &diff.Options)
6559

6660
diff.releases = args[0:]
6761
if isHelm3() {
@@ -74,15 +68,10 @@ func releaseCmd() *cobra.Command {
7468
},
7569
}
7670

77-
releaseCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
78-
releaseCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
7971
releaseCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
80-
releaseCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
81-
releaseCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
8272
releaseCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
83-
releaseCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
84-
releaseCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
8573
releaseCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
74+
AddDiffOptions(releaseCmd.Flags(), &diff.Options)
8675

8776
releaseCmd.SuggestionsMinimumDistance = 1
8877

@@ -121,11 +110,7 @@ func (d *release) differentiateHelm3() error {
121110
seenAnyChanges := diff.Releases(
122111
manifest.Parse(string(releaseResponse1), namespace, d.normalizeManifests, excludes...),
123112
manifest.Parse(string(releaseResponse2), namespace, d.normalizeManifests, excludes...),
124-
d.suppressedKinds,
125-
d.showSecrets,
126-
d.outputContext,
127-
d.output,
128-
d.stripTrailingCR,
113+
&d.Options,
129114
os.Stdout)
130115

131116
if d.detailedExitCode && seenAnyChanges {
@@ -156,11 +141,7 @@ func (d *release) differentiate() error {
156141
seenAnyChanges := diff.Releases(
157142
manifest.ParseRelease(releaseResponse1.Release, d.includeTests, d.normalizeManifests),
158143
manifest.ParseRelease(releaseResponse2.Release, d.includeTests, d.normalizeManifests),
159-
d.suppressedKinds,
160-
d.showSecrets,
161-
d.outputContext,
162-
d.output,
163-
d.stripTrailingCR,
144+
&d.Options,
164145
os.Stdout)
165146

166147
if d.detailedExitCode && seenAnyChanges {

cmd/revision.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ type revision struct {
1717
release string
1818
client helm.Interface
1919
detailedExitCode bool
20-
suppressedKinds []string
2120
revisions []string
22-
outputContext int
2321
includeTests bool
24-
showSecrets bool
25-
output string
26-
stripTrailingCR bool
2722
normalizeManifests bool
23+
diff.Options
2824
}
2925

3026
const revisionCmdLongUsage = `
@@ -68,9 +64,7 @@ func revisionCmd() *cobra.Command {
6864
return errors.New("Too many arguments to Command \"revision\".\nMaximum 3 arguments allowed: release name, revision1, revision2")
6965
}
7066

71-
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
72-
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
73-
}
67+
ProcessDiffOptions(cmd.Flags(), &diff.Options)
7468

7569
diff.release = args[0]
7670
diff.revisions = args[1:]
@@ -84,15 +78,10 @@ func revisionCmd() *cobra.Command {
8478
},
8579
}
8680

87-
revisionCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
88-
revisionCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
8981
revisionCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
90-
revisionCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
91-
revisionCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
9282
revisionCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
93-
revisionCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
94-
revisionCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
9583
revisionCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
84+
AddDiffOptions(revisionCmd.Flags(), &diff.Options)
9685

9786
revisionCmd.SuggestionsMinimumDistance = 1
9887

@@ -126,11 +115,7 @@ func (d *revision) differentiateHelm3() error {
126115
diff.Manifests(
127116
manifest.Parse(string(revisionResponse), namespace, d.normalizeManifests, excludes...),
128117
manifest.Parse(string(releaseResponse), namespace, d.normalizeManifests, excludes...),
129-
d.suppressedKinds,
130-
d.showSecrets,
131-
d.outputContext,
132-
d.output,
133-
d.stripTrailingCR,
118+
&d.Options,
134119
os.Stdout)
135120

136121
case 2:
@@ -153,11 +138,7 @@ func (d *revision) differentiateHelm3() error {
153138
seenAnyChanges := diff.Manifests(
154139
manifest.Parse(string(revisionResponse1), namespace, d.normalizeManifests, excludes...),
155140
manifest.Parse(string(revisionResponse2), namespace, d.normalizeManifests, excludes...),
156-
d.suppressedKinds,
157-
d.showSecrets,
158-
d.outputContext,
159-
d.output,
160-
d.stripTrailingCR,
141+
&d.Options,
161142
os.Stdout)
162143

163144
if d.detailedExitCode && seenAnyChanges {
@@ -193,11 +174,7 @@ func (d *revision) differentiate() error {
193174
diff.Manifests(
194175
manifest.ParseRelease(revisionResponse.Release, d.includeTests, d.normalizeManifests),
195176
manifest.ParseRelease(releaseResponse.Release, d.includeTests, d.normalizeManifests),
196-
d.suppressedKinds,
197-
d.showSecrets,
198-
d.outputContext,
199-
d.output,
200-
d.stripTrailingCR,
177+
&d.Options,
201178
os.Stdout)
202179

203180
case 2:
@@ -220,11 +197,7 @@ func (d *revision) differentiate() error {
220197
seenAnyChanges := diff.Manifests(
221198
manifest.ParseRelease(revisionResponse1.Release, d.includeTests, d.normalizeManifests),
222199
manifest.ParseRelease(revisionResponse2.Release, d.includeTests, d.normalizeManifests),
223-
d.suppressedKinds,
224-
d.showSecrets,
225-
d.outputContext,
226-
d.output,
227-
d.stripTrailingCR,
200+
&d.Options,
228201
os.Stdout)
229202

230203
if d.detailedExitCode && seenAnyChanges {

cmd/rollback.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ type rollback struct {
1717
release string
1818
client helm.Interface
1919
detailedExitCode bool
20-
suppressedKinds []string
2120
revisions []string
22-
outputContext int
2321
includeTests bool
24-
showSecrets bool
25-
output string
26-
stripTrailingCR bool
2722
normalizeManifests bool
23+
diff.Options
2824
}
2925

3026
const rollbackCmdLongUsage = `
@@ -57,9 +53,7 @@ func rollbackCmd() *cobra.Command {
5753
return err
5854
}
5955

60-
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
61-
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
62-
}
56+
ProcessDiffOptions(cmd.Flags(), &diff.Options)
6357

6458
diff.release = args[0]
6559
diff.revisions = args[1:]
@@ -76,15 +70,10 @@ func rollbackCmd() *cobra.Command {
7670
},
7771
}
7872

79-
rollbackCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
80-
rollbackCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
8173
rollbackCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
82-
rollbackCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
83-
rollbackCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
8474
rollbackCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
85-
rollbackCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
86-
rollbackCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
8775
rollbackCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
76+
AddDiffOptions(rollbackCmd.Flags(), &diff.Options)
8877

8978
rollbackCmd.SuggestionsMinimumDistance = 1
9079

@@ -119,11 +108,7 @@ func (d *rollback) backcastHelm3() error {
119108
seenAnyChanges := diff.Manifests(
120109
manifest.Parse(string(releaseResponse), namespace, d.normalizeManifests, excludes...),
121110
manifest.Parse(string(revisionResponse), namespace, d.normalizeManifests, excludes...),
122-
d.suppressedKinds,
123-
d.showSecrets,
124-
d.outputContext,
125-
d.output,
126-
d.stripTrailingCR,
111+
&d.Options,
127112
os.Stdout)
128113

129114
if d.detailedExitCode && seenAnyChanges {
@@ -156,11 +141,7 @@ func (d *rollback) backcast() error {
156141
seenAnyChanges := diff.Manifests(
157142
manifest.ParseRelease(releaseResponse.Release, d.includeTests, d.normalizeManifests),
158143
manifest.ParseRelease(revisionResponse.Release, d.includeTests, d.normalizeManifests),
159-
d.suppressedKinds,
160-
d.showSecrets,
161-
d.outputContext,
162-
d.output,
163-
d.stripTrailingCR,
144+
&d.Options,
164145
os.Stdout)
165146

166147
if d.detailedExitCode && seenAnyChanges {

cmd/upgrade.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,13 @@ type diffCmd struct {
5151
allowUnreleased bool
5252
noHooks bool
5353
includeTests bool
54-
suppressedKinds []string
55-
outputContext int
56-
showSecrets bool
5754
postRenderer string
58-
output string
5955
install bool
60-
stripTrailingCR bool
6156
normalizeManifests bool
6257
threeWayMerge bool
6358
extraAPIs []string
6459
useUpgradeDryRun bool
60+
diff.Options
6561
}
6662

6763
func (d *diffCmd) isAllowUnreleased() bool {
@@ -132,9 +128,7 @@ func newChartCommand() *cobra.Command {
132128
}
133129
}
134130

135-
if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
136-
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
137-
}
131+
ProcessDiffOptions(cmd.Flags(), &diff.Options)
138132

139133
diff.release = args[0]
140134
diff.chart = args[1]
@@ -163,8 +157,6 @@ func newChartCommand() *cobra.Command {
163157
// - https://github.com/helm/helm/blob/d9ffe37d371c9d06448c55c852c800051830e49a/cmd/helm/template.go#L184
164158
// - https://github.com/databus23/helm-diff/issues/318
165159
f.StringArrayVarP(&diff.extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions")
166-
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
167-
f.BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
168160
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
169161
f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
170162
f.StringArrayVar(&diff.stringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
@@ -176,15 +168,14 @@ func newChartCommand() *cobra.Command {
176168
f.BoolVar(&diff.noHooks, "no-hooks", false, "disable diffing of hooks")
177169
f.BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
178170
f.BoolVar(&diff.devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
179-
f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
180-
f.IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
181171
f.BoolVar(&diff.disableValidation, "disable-validation", false, "disables rendered templates validation against the Kubernetes cluster you are currently pointing to. This is the same validation performed on an install")
182172
f.BoolVar(&diff.disableOpenAPIValidation, "disable-openapi-validation", false, "disables rendered templates validation against the Kubernetes OpenAPI Schema")
183173
f.BoolVar(&diff.dryRun, "dry-run", false, "disables cluster access and show diff as if it was install. Implies --install, --reset-values, and --disable-validation")
184174
f.StringVar(&diff.postRenderer, "post-renderer", "", "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
185-
f.StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, json, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
186-
f.BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
187175
f.BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
176+
177+
AddDiffOptions(f, &diff.Options)
178+
188179
if !isHelm3() {
189180
f.StringVar(&diff.namespace, "namespace", "default", "namespace to assume the release to be installed into")
190181
}
@@ -271,7 +262,7 @@ func (d *diffCmd) runHelm3() error {
271262
} else {
272263
newSpecs = manifest.Parse(string(installManifest), d.namespace, d.normalizeManifests, helm3TestHook, helm2TestSuccessHook)
273264
}
274-
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
265+
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, &d.Options, os.Stdout)
275266

276267
if d.detailedExitCode && seenAnyChanges {
277268
return Error{
@@ -463,7 +454,7 @@ func (d *diffCmd) run() error {
463454
}
464455
}
465456

466-
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
457+
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, &d.Options, os.Stdout)
467458

468459
if d.detailedExitCode && seenAnyChanges {
469460
return Error{

0 commit comments

Comments
 (0)