diff --git a/cmd/command.go b/cmd/command.go index 424c5fa..e5e4e1f 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -91,6 +91,18 @@ func Command() *cobra.Command { //nolint:funlen nil, "exclude resources by substring contents") + // Define --diff flag. + mf.StringErrorMatcher(matcher.NewDiffMatcher, + "diff", + "", + "include resources which differ from those in a file") + + // Define --not-diff flag. + mf.StringErrorMatcher(matcher.NewDiffMatcher, + "not-diff", + "", + "exclude resources which differ from those in a file") + // Define --exec flag. mf.StringErrorMatcher(matcher.NewExecMatcher, "exec", diff --git a/matcher/matcher-diff.go b/matcher/matcher-diff.go index ae38238..44a7656 100644 --- a/matcher/matcher-diff.go +++ b/matcher/matcher-diff.go @@ -37,7 +37,8 @@ type diffMatcher struct { func (m diffMatcher) Matches(item resources.Resource) bool { for _, original := range m.originals { switch { - // Check if the resource under investigation is the counterpart of any original resources. + // Check if the resource under investigation is the counterpart of any + // original resources. case item.GetAPIVersion() != original.GetAPIVersion(): continue case item.GetKind() != original.GetKind(): @@ -46,11 +47,14 @@ func (m diffMatcher) Matches(item resources.Resource) bool { continue case item.GetNamespace() != original.GetNamespace(): continue - // Diff the resource under investigation against its original counterpart. - case cmp.Equal(item.Object, original.Object): - return false } + + // Diff the resource under investigation against its original + // counterpart. + return !cmp.Equal(item.Object, original.Object) } + // The resource under investigation did not have an original counterpart, + // and has therefore differed. return true } diff --git a/matcher/matcher-diff_test.go b/matcher/matcher-diff_test.go index 0420559..adf2cfd 100644 --- a/matcher/matcher-diff_test.go +++ b/matcher/matcher-diff_test.go @@ -19,6 +19,7 @@ func TestDiffMatcher(t *testing.T) { title: "diff resources", matcher: must(matcher.NewDiffMatcher("testdata/resources.txt")), matches: []string{ + "ConfigMap/my-configmap", "Deployment/nginx-deployment", "Pod/test-pod", }, diff --git a/matcher/testdata/resources.txt b/matcher/testdata/resources.txt index ee76191..984e367 100644 --- a/matcher/testdata/resources.txt +++ b/matcher/testdata/resources.txt @@ -93,4 +93,4 @@ metadata: labels: app.kubernetes.io/component: auth name: my-configmap - namespace: custom-app + namespace: custom-app-other # <------------------------------- changed value