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
This commit is contained in:
Alexander Campbell 2017-05-25 10:36:42 -07:00
parent 382a170054
commit 0d65d9594c
3 changed files with 12 additions and 4 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}