Skip to content

Commit

Permalink
fix(cli): Split diff and non-diff output (grafana#537)
Browse files Browse the repository at this point in the history
On commands that produce diffs: diff, apply, and prune.

It is desirable for some use cases to parse diff output from `kubectl
diff` (e.g. secret redaction until
kubernetes/kubernetes#87840 is resolved). This
is a lot easier when diff and non-diff output are not combined on the
same channel, stdout.

This commit moves diagnostic, non-diff output for the 3 relevant
subcommands to stderr.
  • Loading branch information
craigfurman authored Mar 16, 2021
1 parent 17684fd commit 5149d1b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/kubernetes/client/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (k Kubectl) Delete(namespace, kind, name string, opts DeleteOpts) error {
}

cmd := k.ctl("delete", argv...)
cmd.Stdout = os.Stdout
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

Expand Down
11 changes: 6 additions & 5 deletions pkg/tanka/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tanka

import (
"fmt"
"log"

"github.com/fatih/color"

Expand Down Expand Up @@ -40,7 +41,7 @@ func Prune(baseDir string, opts PruneOpts) error {
}

if len(orphaned) == 0 {
fmt.Println("Nothing found to prune.")
log.Println("Nothing found to prune.")
return nil
}

Expand All @@ -61,12 +62,12 @@ func Prune(baseDir string, opts PruneOpts) error {
}
}
if len(namespaces) > 0 {
warning := color.New(color.FgHiYellow, color.Bold).PrintFunc()
warning("WARNING: This will delete following namespaces and all resources in them:\n")
warning := color.New(color.FgHiYellow, color.Bold).FprintfFunc()
warning(color.Error, "WARNING: This will delete following namespaces and all resources in them:\n")
for _, ns := range namespaces {
fmt.Printf(" - %s\n", ns)
log.Printf(" - %s\n", ns)
}
fmt.Println("")
log.Println("")
}

// prompt for confirm
Expand Down
3 changes: 2 additions & 1 deletion pkg/tanka/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tanka

import (
"fmt"
"log"

"github.com/fatih/color"

Expand Down Expand Up @@ -45,7 +46,7 @@ func Apply(baseDir string, opts ApplyOpts) error {
switch {
case err != nil:
// This is not fatal, the diff is not strictly required
fmt.Println("Error diffing:", err)
log.Println("Error diffing:", err)
case diff == nil:
tmp := "Warning: There are no differences. Your apply may not do anything at all."
diff = &tmp
Expand Down

0 comments on commit 5149d1b

Please sign in to comment.