Skip to content

Commit 1caa24c

Browse files
authored
feature(output): add dyff powered output (#411)
1 parent e9c59b3 commit 1caa24c

File tree

4 files changed

+133
-29
lines changed

4 files changed

+133
-29
lines changed

cmd/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
1111
f.BoolVar(&o.ShowSecrets, "show-secrets", false, "do not redact secret values in the output")
1212
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
1313
f.IntVarP(&o.OutputContext, "context", "C", -1, "output NUM lines of context around changes")
14-
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.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template, dyff. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
1515
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
1616
f.Float32VarP(&o.FindRenames, "find-renames", "D", 0, "Enable rename detection if set to any value greater than 0. If specified, the value denotes the maximum fraction of changed content as lines added + removed compared to total lines in a diff for considering it a rename. Only objects of the same Kind are attempted to be matched")
1717
}

diff/report.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package diff
33
import (
44
"errors"
55
"fmt"
6+
"github.com/gonvenience/ytbx"
7+
"github.com/homeport/dyff/pkg/dyff"
68
"io"
79
"log"
810
"os"
@@ -61,11 +63,52 @@ func (r *Report) setupReportFormat(format string) {
6163
setupTemplateReport(r)
6264
case "json":
6365
setupJSONReport(r)
66+
case "dyff":
67+
setupDyffReport(r)
6468
default:
6569
setupDiffReport(r)
6670
}
6771
}
6872

73+
func setupDyffReport(r *Report) {
74+
r.format.output = printDyffReport
75+
}
76+
77+
func printDyffReport(r *Report, to io.Writer) {
78+
currentFile, _ := os.CreateTemp("", "existing-values")
79+
defer os.Remove(currentFile.Name())
80+
newFile, _ := os.CreateTemp("", "new-values")
81+
defer os.Remove(newFile.Name())
82+
83+
for _, entry := range r.entries {
84+
_, _ = currentFile.WriteString("---\n")
85+
_, _ = newFile.WriteString("---\n")
86+
for _, record := range entry.diffs {
87+
switch record.Delta {
88+
case difflib.Common:
89+
_, _ = currentFile.WriteString(record.Payload + "\n")
90+
_, _ = newFile.WriteString(record.Payload + "\n")
91+
case difflib.LeftOnly:
92+
_, _ = currentFile.WriteString(record.Payload + "\n")
93+
case difflib.RightOnly:
94+
_, _ = newFile.WriteString(record.Payload + "\n")
95+
}
96+
}
97+
}
98+
_ = currentFile.Close()
99+
_ = newFile.Close()
100+
101+
currentInputFile, newInputFile, _ := ytbx.LoadFiles(currentFile.Name(), newFile.Name())
102+
103+
report, _ := dyff.CompareInputFiles(currentInputFile, newInputFile)
104+
reportWriter := &dyff.HumanReport{
105+
Report: report,
106+
OmitHeader: true,
107+
MinorChangeThreshold: 0.1,
108+
}
109+
_ = reportWriter.WriteReport(to)
110+
}
111+
69112
// addEntry: stores diff changes.
70113
func (r *Report) addEntry(key string, suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, changeType string) {
71114
entry := ReportEntry{

go.mod

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ go 1.17
44

55
require (
66
github.com/Masterminds/semver v1.5.0
7-
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
87
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
98
github.com/evanphx/json-patch v4.12.0+incompatible
109
github.com/ghodss/yaml v1.0.0
11-
github.com/huandu/xstrings v1.3.2 // indirect
1210
github.com/json-iterator/go v1.1.12
13-
github.com/mattn/go-colorable v0.1.12 // indirect
1411
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
1512
github.com/pkg/errors v0.9.1
16-
github.com/spf13/cobra v1.4.0
13+
github.com/spf13/cobra v1.5.0
1714
github.com/spf13/pflag v1.0.5
1815
github.com/stretchr/testify v1.7.2
1916
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
@@ -29,7 +26,12 @@ require (
2926
sigs.k8s.io/yaml v1.3.0
3027
)
3128

32-
require github.com/google/go-cmp v0.5.6
29+
require github.com/google/go-cmp v0.5.8
30+
31+
require (
32+
github.com/gonvenience/ytbx v1.4.4
33+
github.com/homeport/dyff v1.5.6
34+
)
3335

3436
require (
3537
cloud.google.com/go v0.99.0 // indirect
@@ -44,6 +46,7 @@ require (
4446
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
4547
github.com/Masterminds/goutils v1.1.1 // indirect
4648
github.com/Masterminds/semver/v3 v3.1.1 // indirect
49+
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
4750
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
4851
github.com/Masterminds/squirrel v1.5.3 // indirect
4952
github.com/PuerkitoBio/purell v1.1.1 // indirect
@@ -75,6 +78,11 @@ require (
7578
github.com/gogo/protobuf v1.3.2 // indirect
7679
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
7780
github.com/golang/protobuf v1.5.2 // indirect
81+
github.com/gonvenience/bunt v1.3.4 // indirect
82+
github.com/gonvenience/neat v1.3.11 // indirect
83+
github.com/gonvenience/term v1.0.2 // indirect
84+
github.com/gonvenience/text v1.0.7 // indirect
85+
github.com/gonvenience/wrap v1.1.2 // indirect
7886
github.com/google/btree v1.0.1 // indirect
7987
github.com/google/gnostic v0.5.7-v3refs // indirect
8088
github.com/google/gofuzz v1.2.0 // indirect
@@ -83,6 +91,7 @@ require (
8391
github.com/gorilla/mux v1.8.0 // indirect
8492
github.com/gosuri/uitable v0.0.4 // indirect
8593
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
94+
github.com/huandu/xstrings v1.3.2 // indirect
8695
github.com/imdario/mergo v0.3.12 // indirect
8796
github.com/inconshreveable/mousetrap v1.0.0 // indirect
8897
github.com/jmoiron/sqlx v1.3.5 // indirect
@@ -92,12 +101,17 @@ require (
92101
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
93102
github.com/lib/pq v1.10.6 // indirect
94103
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
104+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
95105
github.com/mailru/easyjson v0.7.6 // indirect
106+
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
107+
github.com/mattn/go-colorable v0.1.12 // indirect
96108
github.com/mattn/go-isatty v0.0.14 // indirect
97109
github.com/mattn/go-runewidth v0.0.9 // indirect
98110
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
99111
github.com/mitchellh/copystructure v1.2.0 // indirect
112+
github.com/mitchellh/go-ps v1.0.0 // indirect
100113
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
114+
github.com/mitchellh/hashstructure v1.1.0 // indirect
101115
github.com/mitchellh/reflectwalk v1.0.2 // indirect
102116
github.com/moby/locker v1.0.1 // indirect
103117
github.com/moby/spdystream v0.2.0 // indirect
@@ -117,20 +131,23 @@ require (
117131
github.com/prometheus/procfs v0.7.3 // indirect
118132
github.com/rubenv/sql-migrate v1.1.1 // indirect
119133
github.com/russross/blackfriday v1.5.2 // indirect
134+
github.com/sergi/go-diff v1.2.0 // indirect
120135
github.com/shopspring/decimal v1.2.0 // indirect
121136
github.com/sirupsen/logrus v1.8.1 // indirect
122137
github.com/spf13/cast v1.4.1 // indirect
138+
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
139+
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect
123140
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
124141
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
125142
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
126143
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
127144
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
128-
golang.org/x/net v0.7.0 // indirect
145+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
129146
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
130-
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
131-
golang.org/x/sys v0.5.0 // indirect
132-
golang.org/x/term v0.5.0 // indirect
133-
golang.org/x/text v0.7.0 // indirect
147+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
148+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
149+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
150+
golang.org/x/text v0.3.7 // indirect
134151
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
135152
google.golang.org/appengine v1.6.7 // indirect
136153
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect

0 commit comments

Comments
 (0)