Skip to content

Commit

Permalink
Feat(between): add ignore value changes flag
Browse files Browse the repository at this point in the history
Sometimes it is useful to only include changes in structure and ignore the changes in values. Adding a flag to drop such changes in the report.
-v --ignore-value-chnages
  • Loading branch information
Siavosh Kasravi committed Dec 11, 2024
1 parent 5a31825 commit 6fe62db
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/cmd/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand Down
19 changes: 19 additions & 0 deletions internal/cmd/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type reportConfig struct {
exitWithCode bool
omitHeader bool
useGoPatchPaths bool
ignoreValueChanges bool
minorChangeThreshold float64
multilineContextLines int
additionalIdentifiers []string
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions pkg/dyff/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 _, detail := range diff.Details {
if detail.Kind == MODIFICATION {
hasValChange = true
break
}
}

if !hasValChange {
result.Diffs = append(result.Diffs, diff)
}
}

return result
}

0 comments on commit 6fe62db

Please sign in to comment.