From 18ce1f03862c9315b5a00d8bb69f30c21711f5e0 Mon Sep 17 00:00:00 2001 From: Siavosh Kasravi Date: Sat, 23 Nov 2024 16:56:00 +0330 Subject: [PATCH] feat(between): add ignore value changes flag --- internal/cmd/between.go | 4 ++++ internal/cmd/cmds_test.go | 19 +++++++++++++++++++ internal/cmd/common.go | 3 ++- pkg/dyff/reports.go | 23 +++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/cmd/between.go b/internal/cmd/between.go index 23665d8..b1beb38 100644 --- a/internal/cmd/between.go +++ b/internal/cmd/between.go @@ -111,6 +111,10 @@ types are: YAML (http://yaml.org/) and JSON (http://json.org/). report = report.ExcludeRegexp(reportOptions.excludeRegexps...) } + if reportOptions.ignoreValueChanges { + report = report.IgnoreValueChanges() + } + return writeReport(cmd, report) }, } diff --git a/internal/cmd/cmds_test.go b/internal/cmd/cmds_test.go index a78b239..f122540 100644 --- a/internal/cmd/cmds_test.go +++ b/internal/cmd/cmds_test.go @@ -591,6 +591,25 @@ spec.replicas (apps/v1/Deployment/test) Expect(err).ToNot(HaveOccurred()) Expect(out).To(BeEquivalentTo("\n")) }) + + It("should ignore the changes in values", func() { + expected := ` +(file level) + - one document removed: + --- + apiVersion: v1 + kind: Namespace + metadata: + name: test + +` + By("using the --ignore-value-changes", func() { + out, err := dyff("between", "--omit-header", "--ignore-value-changes", assets("issues", "issue-232", "from.yml"), assets("issues", "issue-232", "to.yml")) + Expect(err).ToNot(HaveOccurred()) + Expect(out).To(BeEquivalentTo(expected)) + }) + + }) }) Context("last-applied command", func() { diff --git a/internal/cmd/common.go b/internal/cmd/common.go index 9b0b47c..c30608f 100644 --- a/internal/cmd/common.go +++ b/internal/cmd/common.go @@ -47,6 +47,7 @@ type reportConfig struct { exitWithCode bool omitHeader bool useGoPatchPaths bool + ignoreValueChanges bool minorChangeThreshold float64 multilineContextLines int additionalIdentifiers []string @@ -87,7 +88,7 @@ func applyReportOptionsFlags(cmd *cobra.Command) { cmd.Flags().StringSliceVar(&reportOptions.excludes, "exclude", defaults.excludes, "exclude reports from a set of differences based on supplied arguments") cmd.Flags().StringSliceVar(&reportOptions.filterRegexps, "filter-regexp", defaults.filterRegexps, "filter reports to a subset of differences based on supplied regular expressions") cmd.Flags().StringSliceVar(&reportOptions.excludeRegexps, "exclude-regexp", defaults.excludeRegexps, "exclude reports from a set of differences based on supplied regular expressions") - + cmd.Flags().BoolVarP(&reportOptions.ignoreValueChanges, "ignore-value-changes", "v", false, "exclude changes in values") // Main output preferences cmd.Flags().StringVarP(&reportOptions.style, "output", "o", defaults.style, "specify the output style, supported styles: human, brief, github, gitlab, gitea") cmd.Flags().BoolVarP(&reportOptions.omitHeader, "omit-header", "b", defaults.omitHeader, "omit the dyff summary header") diff --git a/pkg/dyff/reports.go b/pkg/dyff/reports.go index 710904c..7c6e7d1 100644 --- a/pkg/dyff/reports.go +++ b/pkg/dyff/reports.go @@ -98,3 +98,26 @@ func (r Report) ExcludeRegexp(pattern ...string) (result Report) { return true }) } + +func (r Report) IgnoreValueChanges() (result Report) { + result = Report{ + From: r.From, + To: r.To, + } + + for _, diff := range r.Diffs { + var hasValChange = false + for _, t := range diff.Details { + if t.Kind == '±' { + hasValChange = true + break + } + } + + if !hasValChange { + result.Diffs = append(result.Diffs, diff) + } + } + + return result +}