Skip to content

add support for strip-trailing-cr #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Flags:
--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
--reset-values reset the values to the ones built into the chart and merge in any new values
--reuse-values reuse the last release's values and merge in any new values
--strip-trailing-cr strip trailing carriage return on input
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--suppress stringArray allows suppression of the values listed in the diff output
-q, --suppress-secrets suppress secrets in the output
Expand Down Expand Up @@ -135,6 +136,7 @@ Flags:
-h, --help help for release
--home string location of your Helm config. Overrides $HELM_HOME (default "/home/aananth/.helm")
--include-tests enable the diffing of the helm test hooks
--strip-trailing-cr strip trailing carriage return on input
--suppress stringArray allows suppression of the values listed in the diff output
-q, --suppress-secrets suppress secrets in the output
--tls enable TLS for request
Expand Down Expand Up @@ -172,6 +174,7 @@ Usage:

Flags:
-h, --help help for revision
--strip-trailing-cr strip trailing carriage return on input
--suppress stringArray allows suppression of the values listed in the diff output
-q, --suppress-secrets suppress secrets in the output

Expand All @@ -197,6 +200,7 @@ Examples:

Flags:
-h, --help help for rollback
--strip-trailing-cr strip trailing carriage return on input
--suppress stringArray allows suppression of the values listed in the diff output
-q, --suppress-secrets suppress secrets in the output

Expand Down
4 changes: 4 additions & 0 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type release struct {
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
}

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

releaseCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -121,6 +123,7 @@ func (d *release) differentiateHelm3() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -155,6 +158,7 @@ func (d *release) differentiate() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
6 changes: 6 additions & 0 deletions cmd/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type revision struct {
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
}

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

revisionCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -126,6 +128,7 @@ func (d *revision) differentiateHelm3() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

case 2:
Expand All @@ -152,6 +155,7 @@ func (d *revision) differentiateHelm3() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -191,6 +195,7 @@ func (d *revision) differentiate() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

case 2:
Expand All @@ -217,6 +222,7 @@ func (d *revision) differentiate() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
4 changes: 4 additions & 0 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type rollback struct {
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
}

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

rollbackCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -119,6 +121,7 @@ func (d *rollback) backcastHelm3() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -155,6 +158,7 @@ func (d *rollback) backcast() error {
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
6 changes: 4 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type diffCmd struct {
postRenderer string
output string
install bool
stripTrailingCR bool
}

func (d *diffCmd) isAllowUnreleased() bool {
Expand Down Expand Up @@ -117,6 +118,7 @@ func newChartCommand() *cobra.Command {
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")
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")
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.")
f.BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
if !isHelm3() {
f.StringVar(&diff.namespace, "namespace", "default", "namespace to assume the release to be installed into")
}
Expand Down Expand Up @@ -184,7 +186,7 @@ func (d *diffCmd) runHelm3() error {
} else {
newSpecs = manifest.Parse(string(installManifest), d.namespace, helm3TestHook, helm2TestSuccessHook)
}
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, os.Stdout)
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)

if d.detailedExitCode && seenAnyChanges {
return Error{
Expand Down Expand Up @@ -270,7 +272,7 @@ func (d *diffCmd) run() error {
}
}

seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, os.Stdout)
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)

if d.detailedExitCode && seenAnyChanges {
return Error{
Expand Down
32 changes: 22 additions & 10 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

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

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

}
diffs := diffMappingResults(oldContent, emptyMapping)
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
if len(diffs) > 0 {
seenAnyChanges = true
}
Expand All @@ -61,7 +61,7 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressed
if !showSecrets {
redactSecrets(nil, newContent)
}
diffs := diffMappingResults(emptyMapping, newContent)
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
if len(diffs) > 0 {
seenAnyChanges = true
}
Expand Down Expand Up @@ -142,19 +142,31 @@ func getComment(s string) string {
}

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

func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult) []difflib.DiffRecord {
return diffStrings(oldContent.Content, newContent.Content)
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult, stripTrailingCR bool ) []difflib.DiffRecord {
return diffStrings(oldContent.Content, newContent.Content, stripTrailingCR)
}

func diffStrings(before, after string) []difflib.DiffRecord {
func diffStrings(before, after string, stripTrailingCR bool) []difflib.DiffRecord {
return difflib.Diff(split(before, stripTrailingCR), split(after, stripTrailingCR))
}

func split(value string, stripTrailingCR bool) []string {
const sep = "\n"
return difflib.Diff(strings.Split(before, sep), strings.Split(after, sep))
split := strings.Split(value, sep)
if !stripTrailingCR {
return split
}
var stripped []string
for _, s := range split {
stripped = append(stripped, strings.TrimSuffix(s, "\r"))
}
return stripped
}

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