Skip to content

Commit 818e596

Browse files
authored
Add support for strip-trailing-cr (#237)
* add support for strip-trailing-cr Fixes #144
1 parent 0c4bd34 commit 818e596

File tree

7 files changed

+99
-27
lines changed

7 files changed

+99
-27
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Flags:
124124
--post-renderer string 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
125125
--reset-values reset the values to the ones built into the chart and merge in any new values
126126
--reuse-values reuse the last release's values and merge in any new values
127+
--strip-trailing-cr strip trailing carriage return on input
127128
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
128129
--suppress stringArray allows suppression of the values listed in the diff output
129130
-q, --suppress-secrets suppress secrets in the output
@@ -156,6 +157,7 @@ Flags:
156157
-h, --help help for release
157158
--home string location of your Helm config. Overrides $HELM_HOME (default "/home/aananth/.helm")
158159
--include-tests enable the diffing of the helm test hooks
160+
--strip-trailing-cr strip trailing carriage return on input
159161
--suppress stringArray allows suppression of the values listed in the diff output
160162
-q, --suppress-secrets suppress secrets in the output
161163
--tls enable TLS for request
@@ -193,6 +195,7 @@ Usage:
193195
194196
Flags:
195197
-h, --help help for revision
198+
--strip-trailing-cr strip trailing carriage return on input
196199
--suppress stringArray allows suppression of the values listed in the diff output
197200
-q, --suppress-secrets suppress secrets in the output
198201
@@ -218,6 +221,7 @@ Examples:
218221
219222
Flags:
220223
-h, --help help for rollback
224+
--strip-trailing-cr strip trailing carriage return on input
221225
--suppress stringArray allows suppression of the values listed in the diff output
222226
-q, --suppress-secrets suppress secrets in the output
223227

cmd/release.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type release struct {
2121
includeTests bool
2222
showSecrets bool
2323
output string
24+
stripTrailingCR bool
2425
}
2526

2627
const releaseCmdLongUsage = `
@@ -79,6 +80,7 @@ func releaseCmd() *cobra.Command {
7980
releaseCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
8081
releaseCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
8182
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.")
83+
releaseCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
8284

8385
releaseCmd.SuggestionsMinimumDistance = 1
8486

@@ -121,6 +123,7 @@ func (d *release) differentiateHelm3() error {
121123
d.showSecrets,
122124
d.outputContext,
123125
d.output,
126+
d.stripTrailingCR,
124127
os.Stdout)
125128

126129
if d.detailedExitCode && seenAnyChanges {
@@ -155,6 +158,7 @@ func (d *release) differentiate() error {
155158
d.showSecrets,
156159
d.outputContext,
157160
d.output,
161+
d.stripTrailingCR,
158162
os.Stdout)
159163

160164
if d.detailedExitCode && seenAnyChanges {

cmd/revision.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type revision struct {
2323
includeTests bool
2424
showSecrets bool
2525
output string
26+
stripTrailingCR bool
2627
}
2728

2829
const revisionCmdLongUsage = `
@@ -89,6 +90,7 @@ func revisionCmd() *cobra.Command {
8990
revisionCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
9091
revisionCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
9192
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.")
93+
revisionCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
9294

9395
revisionCmd.SuggestionsMinimumDistance = 1
9496

@@ -126,6 +128,7 @@ func (d *revision) differentiateHelm3() error {
126128
d.showSecrets,
127129
d.outputContext,
128130
d.output,
131+
d.stripTrailingCR,
129132
os.Stdout)
130133

131134
case 2:
@@ -152,6 +155,7 @@ func (d *revision) differentiateHelm3() error {
152155
d.showSecrets,
153156
d.outputContext,
154157
d.output,
158+
d.stripTrailingCR,
155159
os.Stdout)
156160

157161
if d.detailedExitCode && seenAnyChanges {
@@ -191,6 +195,7 @@ func (d *revision) differentiate() error {
191195
d.showSecrets,
192196
d.outputContext,
193197
d.output,
198+
d.stripTrailingCR,
194199
os.Stdout)
195200

196201
case 2:
@@ -217,6 +222,7 @@ func (d *revision) differentiate() error {
217222
d.showSecrets,
218223
d.outputContext,
219224
d.output,
225+
d.stripTrailingCR,
220226
os.Stdout)
221227

222228
if d.detailedExitCode && seenAnyChanges {

cmd/rollback.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type rollback struct {
2323
includeTests bool
2424
showSecrets bool
2525
output string
26+
stripTrailingCR bool
2627
}
2728

2829
const rollbackCmdLongUsage = `
@@ -81,6 +82,7 @@ func rollbackCmd() *cobra.Command {
8182
rollbackCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
8283
rollbackCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
8384
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.")
85+
rollbackCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
8486

8587
rollbackCmd.SuggestionsMinimumDistance = 1
8688

@@ -119,6 +121,7 @@ func (d *rollback) backcastHelm3() error {
119121
d.showSecrets,
120122
d.outputContext,
121123
d.output,
124+
d.stripTrailingCR,
122125
os.Stdout)
123126

124127
if d.detailedExitCode && seenAnyChanges {
@@ -155,6 +158,7 @@ func (d *rollback) backcast() error {
155158
d.showSecrets,
156159
d.outputContext,
157160
d.output,
161+
d.stripTrailingCR,
158162
os.Stdout)
159163

160164
if d.detailedExitCode && seenAnyChanges {

cmd/upgrade.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type diffCmd struct {
4040
postRenderer string
4141
output string
4242
install bool
43+
stripTrailingCR bool
4344
}
4445

4546
func (d *diffCmd) isAllowUnreleased() bool {
@@ -119,6 +120,7 @@ func newChartCommand() *cobra.Command {
119120
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")
120121
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")
121122
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.")
123+
f.BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
122124
if !isHelm3() {
123125
f.StringVar(&diff.namespace, "namespace", "default", "namespace to assume the release to be installed into")
124126
}
@@ -186,7 +188,7 @@ func (d *diffCmd) runHelm3() error {
186188
} else {
187189
newSpecs = manifest.Parse(string(installManifest), d.namespace, helm3TestHook, helm2TestSuccessHook)
188190
}
189-
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, os.Stdout)
191+
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
190192

191193
if d.detailedExitCode && seenAnyChanges {
192194
return Error{
@@ -272,7 +274,7 @@ func (d *diffCmd) run() error {
272274
}
273275
}
274276

275-
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, os.Stdout)
277+
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
276278

277279
if d.detailedExitCode && seenAnyChanges {
278280
return Error{

diff/diff.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
// Manifests diff on manifests
22-
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, to io.Writer) bool {
22+
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, stripTrailingCR bool, to io.Writer) bool {
2323
report.setupReportFormat(output)
2424
seenAnyChanges := false
2525
emptyMapping := &manifest.MappingResult{}
@@ -33,7 +33,7 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
3333
redactSecrets(oldContent, newContent)
3434
}
3535

36-
diffs := diffMappingResults(oldContent, newContent)
36+
diffs := diffMappingResults(oldContent, newContent, stripTrailingCR)
3737
if len(diffs) > 0 {
3838
seenAnyChanges = true
3939
}
@@ -45,7 +45,7 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
4545
redactSecrets(oldContent, nil)
4646

4747
}
48-
diffs := diffMappingResults(oldContent, emptyMapping)
48+
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
4949
if len(diffs) > 0 {
5050
seenAnyChanges = true
5151
}
@@ -61,7 +61,7 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
6161
if !showSecrets {
6262
redactSecrets(nil, newContent)
6363
}
64-
diffs := diffMappingResults(emptyMapping, newContent)
64+
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
6565
if len(diffs) > 0 {
6666
seenAnyChanges = true
6767
}
@@ -142,19 +142,31 @@ func getComment(s string) string {
142142
}
143143

144144
// Releases reindex the content based on the template names and pass it to Manifests
145-
func Releases(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, to io.Writer) bool {
145+
func Releases(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, stripTrailingCR bool, to io.Writer) bool {
146146
oldIndex = reIndexForRelease(oldIndex)
147147
newIndex = reIndexForRelease(newIndex)
148-
return Manifests(oldIndex, newIndex, suppressedKinds, showSecrets, context, output, to)
148+
return Manifests(oldIndex, newIndex, suppressedKinds, showSecrets, context, output, stripTrailingCR, to)
149149
}
150150

151-
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
152-
return diffStrings(oldContent.Content, newContent.Content)
151+
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult, stripTrailingCR bool ) []difflib.DiffRecord {
152+
return diffStrings(oldContent.Content, newContent.Content, stripTrailingCR)
153153
}
154154

155-
func diffStrings(before, after string) []difflib.DiffRecord {
155+
func diffStrings(before, after string, stripTrailingCR bool) []difflib.DiffRecord {
156+
return difflib.Diff(split(before, stripTrailingCR), split(after, stripTrailingCR))
157+
}
158+
159+
func split(value string, stripTrailingCR bool) []string {
156160
const sep = "\n"
157-
return difflib.Diff(strings.Split(before, sep), strings.Split(after, sep))
161+
split := strings.Split(value, sep)
162+
if !stripTrailingCR {
163+
return split
164+
}
165+
var stripped []string
166+
for _, s := range split {
167+
stripped = append(stripped, strings.TrimSuffix(s, "\r"))
168+
}
169+
return stripped
158170
}
159171

160172
func printDiffRecords(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {

0 commit comments

Comments
 (0)