From 5149d1ba761db1c519c65f4502edf715cb8bc31e Mon Sep 17 00:00:00 2001 From: Craig Furman Date: Tue, 16 Mar 2021 14:46:09 +0000 Subject: [PATCH] fix(cli): Split diff and non-diff output (#537) 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 https://github.com/kubernetes/kubernetes/issues/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. --- pkg/kubernetes/client/delete.go | 2 +- pkg/tanka/prune.go | 11 ++++++----- pkg/tanka/workflow.go | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/kubernetes/client/delete.go b/pkg/kubernetes/client/delete.go index 55a2593c2..1577beb67 100644 --- a/pkg/kubernetes/client/delete.go +++ b/pkg/kubernetes/client/delete.go @@ -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 diff --git a/pkg/tanka/prune.go b/pkg/tanka/prune.go index 16a4c3117..78b8a34c0 100644 --- a/pkg/tanka/prune.go +++ b/pkg/tanka/prune.go @@ -2,6 +2,7 @@ package tanka import ( "fmt" + "log" "github.com/fatih/color" @@ -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 } @@ -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 diff --git a/pkg/tanka/workflow.go b/pkg/tanka/workflow.go index 8c40e46aa..c72b2faab 100644 --- a/pkg/tanka/workflow.go +++ b/pkg/tanka/workflow.go @@ -2,6 +2,7 @@ package tanka import ( "fmt" + "log" "github.com/fatih/color" @@ -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