diff --git a/cmd/diff.go b/cmd/diff.go index 27ce1d6a..141887e7 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -24,11 +24,13 @@ import ( const ( flagDiffStrategy = "diff-strategy" flagIgnore = "ignore" + flagOmitSecrets = "omit-secrets" ) func init() { diffCmd.PersistentFlags().String(flagDiffStrategy, "all", "Diff strategy, all or subset.") diffCmd.PersistentFlags().StringArray(flagIgnore, []string{}, "JSON Path to ignore when calculating diffs.") + diffCmd.PersistentFlags().Bool(flagOmitSecrets, false, "hide secret details when showing diff") RootCmd.AddCommand(diffCmd) } @@ -52,6 +54,11 @@ var diffCmd = &cobra.Command{ return err } + c.OmitSecrets, err = flags.GetBool(flagOmitSecrets) + if err != nil { + return err + } + c.Client, c.Mapper, _, err = getDynamicClients(cmd) if err != nil { return err diff --git a/pkg/kubecfg/diff.go b/pkg/kubecfg/diff.go index 8846349a..0c5dd61f 100644 --- a/pkg/kubecfg/diff.go +++ b/pkg/kubecfg/diff.go @@ -23,6 +23,7 @@ import ( "os" "regexp" "sort" + "strings" isatty "github.com/mattn/go-isatty" "github.com/sergi/go-diff/diffmatchpatch" @@ -49,6 +50,7 @@ type DiffCmd struct { Mapper meta.RESTMapper DefaultNamespace string IgnorePaths []string + OmitSecrets bool DiffStrategy string } @@ -120,7 +122,7 @@ func (c DiffCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) err fmt.Fprintf(out, "%s unchanged\n", desc) } else { diffFound = true - text := c.formatDiff(diff, isatty.IsTerminal(os.Stdout.Fd())) + text := c.formatDiff(diff, isatty.IsTerminal(os.Stdout.Fd()), c.OmitSecrets && obj.GetKind() == "Secret") fmt.Fprintf(out, "%s\n", text) } } @@ -132,12 +134,16 @@ func (c DiffCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) err } // Formats the supplied Diff as a unified-diff-like text with infinite context and optionally colorizes it. -func (c DiffCmd) formatDiff(diffs []diffmatchpatch.Diff, color bool) string { +func (c DiffCmd) formatDiff(diffs []diffmatchpatch.Diff, color bool, omitchanges bool) string { var buff bytes.Buffer for _, diff := range diffs { text := diff.Text + if omitchanges { + parts := strings.Split(text, ":") + text = parts[0] + ": \n" + } switch diff.Type { case diffmatchpatch.DiffInsert: if color { @@ -156,7 +162,9 @@ func (c DiffCmd) formatDiff(diffs []diffmatchpatch.Diff, color bool) string { _, _ = buff.WriteString("\x1b[0m") } case diffmatchpatch.DiffEqual: - _, _ = buff.WriteString(DiffLineStart.ReplaceAllString(text, "$1 $2")) + if !omitchanges { + _, _ = buff.WriteString(DiffLineStart.ReplaceAllString(text, "$1 $2")) + } } }