diff --git a/pkg/cmd/config/get_contexts.go b/pkg/cmd/config/get_contexts.go index d2f7a987..dbd6f967 100644 --- a/pkg/cmd/config/get_contexts.go +++ b/pkg/cmd/config/get_contexts.go @@ -73,16 +73,7 @@ func NewCmdConfigGetContexts(streams genericclioptions.IOStreams, configAccess c Long: getContextsLong, Example: getContextsExample, Run: func(cmd *cobra.Command, args []string) { - validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "custom-columns", "custom-columns-file", "go-template", "go-template-file", "jsonpath", "jsonpath-file") - supportedOutputTypes := sets.NewString("", "name") - outputFormat := cmdutil.GetFlagString(cmd, "output") - if !validOutputTypes.Has(outputFormat) { - cmdutil.CheckErr(fmt.Errorf("output must be one of '' or 'name': %v", outputFormat)) - } - if !supportedOutputTypes.Has(outputFormat) { - fmt.Fprintf(options.Out, "--output %v is not available in kubectl config get-contexts; resetting to default output format\n", outputFormat) - cmd.Flags().Set("output", "") - } + cmdutil.CheckErr(options.Validate(cmd)) cmdutil.CheckErr(options.Complete(cmd, args)) cmdutil.CheckErr(options.RunGetContexts()) }, @@ -108,6 +99,21 @@ func (o *GetContextsOptions) Complete(cmd *cobra.Command, args []string) error { return nil } +// Validate ensures the of output format +func (o *GetContextsOptions) Validate(cmd *cobra.Command) error { + validOutputTypes := sets.NewString("", "json", "yaml", "wide", "name", "custom-columns", "custom-columns-file", "go-template", "go-template-file", "jsonpath", "jsonpath-file") + supportedOutputTypes := sets.NewString("", "name") + outputFormat := cmdutil.GetFlagString(cmd, "output") + if !validOutputTypes.Has(outputFormat) { + return fmt.Errorf("output must be one of '' or 'name': %v", outputFormat) + } + if !supportedOutputTypes.Has(outputFormat) { + cmd.Flags().Set("output", "") + return fmt.Errorf("--output %v is not available in kubectl config get-contexts; resetting to default output format", outputFormat) + } + return nil +} + // RunGetContexts implements all the necessary functionality for context retrieval. func (o GetContextsOptions) RunGetContexts() error { config, err := o.configAccess.GetStartingConfig()