Skip to content

feature(output): add dyff powered output #411

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 1 commit into from
Apr 23, 2023
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
2 changes: 1 addition & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
f.BoolVar(&o.ShowSecrets, "show-secrets", false, "do not redact secret values in the output")
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
f.IntVarP(&o.OutputContext, "context", "C", -1, "output NUM lines of context around changes")
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.")
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.")
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
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")
}
Expand Down
43 changes: 43 additions & 0 deletions diff/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package diff
import (
"errors"
"fmt"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
"io"
"log"
"os"
Expand Down Expand Up @@ -61,11 +63,52 @@ func (r *Report) setupReportFormat(format string) {
setupTemplateReport(r)
case "json":
setupJSONReport(r)
case "dyff":
setupDyffReport(r)
default:
setupDiffReport(r)
}
}

func setupDyffReport(r *Report) {
r.format.output = printDyffReport
}

func printDyffReport(r *Report, to io.Writer) {
currentFile, _ := os.CreateTemp("", "existing-values")
defer os.Remove(currentFile.Name())
newFile, _ := os.CreateTemp("", "new-values")
defer os.Remove(newFile.Name())

for _, entry := range r.entries {
_, _ = currentFile.WriteString("---\n")
_, _ = newFile.WriteString("---\n")
for _, record := range entry.diffs {
switch record.Delta {
case difflib.Common:
_, _ = currentFile.WriteString(record.Payload + "\n")
_, _ = newFile.WriteString(record.Payload + "\n")
case difflib.LeftOnly:
_, _ = currentFile.WriteString(record.Payload + "\n")
case difflib.RightOnly:
_, _ = newFile.WriteString(record.Payload + "\n")
}
}
}
_ = currentFile.Close()
_ = newFile.Close()

currentInputFile, newInputFile, _ := ytbx.LoadFiles(currentFile.Name(), newFile.Name())

report, _ := dyff.CompareInputFiles(currentInputFile, newInputFile)
reportWriter := &dyff.HumanReport{
Report: report,
OmitHeader: true,
MinorChangeThreshold: 0.1,
}
_ = reportWriter.WriteReport(to)
}

// addEntry: stores diff changes.
func (r *Report) addEntry(key string, suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, changeType string) {
entry := ReportEntry{
Expand Down
37 changes: 27 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ go 1.17

require (
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
github.com/evanphx/json-patch v4.12.0+incompatible
github.com/ghodss/yaml v1.0.0
github.com/huandu/xstrings v1.3.2 // indirect
github.com/json-iterator/go v1.1.12
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.4.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.2
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
Expand All @@ -29,7 +26,12 @@ require (
sigs.k8s.io/yaml v1.3.0
)

require github.com/google/go-cmp v0.5.6
require github.com/google/go-cmp v0.5.8

require (
github.com/gonvenience/ytbx v1.4.4
github.com/homeport/dyff v1.5.6
)

require (
cloud.google.com/go v0.99.0 // indirect
Expand All @@ -44,6 +46,7 @@ require (
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/Masterminds/squirrel v1.5.3 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
Expand Down Expand Up @@ -75,6 +78,11 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gonvenience/bunt v1.3.4 // indirect
github.com/gonvenience/neat v1.3.11 // indirect
github.com/gonvenience/term v1.0.2 // indirect
github.com/gonvenience/text v1.0.7 // indirect
github.com/gonvenience/wrap v1.1.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -83,6 +91,7 @@ require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
Expand All @@ -92,12 +101,17 @@ require (
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
Expand All @@ -117,20 +131,23 @@ require (
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rubenv/sql-migrate v1.1.1 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/texttheater/golang-levenshtein v1.0.1 // indirect
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
Expand Down
Loading