Skip to content

Commit

Permalink
merge release
Browse files Browse the repository at this point in the history
  • Loading branch information
theSuess committed Aug 23, 2019
2 parents a1e49f4 + 6ad6ebe commit f89d263
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
14 changes: 11 additions & 3 deletions pkg/kubecfg/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"regexp"
"sort"
"strings"

isatty "github.com/mattn/go-isatty"
"github.com/sergi/go-diff/diffmatchpatch"
Expand All @@ -49,6 +50,7 @@ type DiffCmd struct {
Mapper meta.RESTMapper
DefaultNamespace string
IgnorePaths []string
OmitSecrets bool

DiffStrategy string
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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] + ": <omitted>\n"
}
switch diff.Type {
case diffmatchpatch.DiffInsert:
if color {
Expand All @@ -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"))
}
}
}

Expand Down

0 comments on commit f89d263

Please sign in to comment.