Merge pull request #93109 from zhouya0/fix_kubectl_config_error_message

Move kubectl get-context validate logic to Validate function
This commit is contained in:
Kubernetes Prow Robot 2020-11-06 09:02:53 -08:00 committed by GitHub
commit 1576823c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()