From 0d65d9594c3d7221c532706d2f64dfaf77ec003d Mon Sep 17 00:00:00 2001 From: Alexander Campbell Date: Thu, 25 May 2017 10:36:42 -0700 Subject: [PATCH] cmd/options: `kubectl options` writes to `out` stream Previous behavior was to write to stderr (thanks to the fallback system in the Cobra library), which made it difficult to grep for flags. For example: kubectl options | grep recursive --- federation/pkg/kubefed/kubefed.go | 2 +- pkg/kubectl/cmd/cmd.go | 2 +- pkg/kubectl/cmd/options.go | 12 ++++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/federation/pkg/kubefed/kubefed.go b/federation/pkg/kubefed/kubefed.go index 6a35d6daf50..8676e2ae642 100644 --- a/federation/pkg/kubefed/kubefed.go +++ b/federation/pkg/kubefed/kubefed.go @@ -67,7 +67,7 @@ func NewKubeFedCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer, defa templates.ActsAsRootCommand(cmds, filters, groups...) cmds.AddCommand(kubectl.NewCmdVersion(f, out)) - cmds.AddCommand(kubectl.NewCmdOptions()) + cmds.AddCommand(kubectl.NewCmdOptions(out)) return cmds } diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 91639dddfda..4bda4489cc2 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -378,7 +378,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob cmds.AddCommand(NewCmdVersion(f, out)) cmds.AddCommand(NewCmdApiVersions(f, out)) cmds.AddCommand(deprecatedAlias("apiversions", NewCmdApiVersions(f, out))) - cmds.AddCommand(NewCmdOptions()) + cmds.AddCommand(NewCmdOptions(out)) return cmds } diff --git a/pkg/kubectl/cmd/options.go b/pkg/kubectl/cmd/options.go index 0512789999d..945e83ded90 100644 --- a/pkg/kubectl/cmd/options.go +++ b/pkg/kubectl/cmd/options.go @@ -17,6 +17,8 @@ limitations under the License. package cmd import ( + "io" + "k8s.io/kubernetes/pkg/kubectl/cmd/templates" "k8s.io/kubernetes/pkg/util/i18n" @@ -30,7 +32,7 @@ var ( ) // NewCmdOptions implements the options command -func NewCmdOptions() *cobra.Command { +func NewCmdOptions(out io.Writer) *cobra.Command { cmd := &cobra.Command{ Use: "options", Short: i18n.T("Print the list of flags inherited by all commands"), @@ -41,7 +43,13 @@ func NewCmdOptions() *cobra.Command { }, } - templates.UseOptionsTemplates(cmd) + // The `options` command needs write its output to the `out` stream + // (typically stdout). Without calling SetOutput here, the Usage() + // function call will fall back to stderr. + // + // See https://github.com/kubernetes/kubernetes/pull/46394 for details. + cmd.SetOutput(out) + templates.UseOptionsTemplates(cmd) return cmd }