Move kubectl get-context validate logic to Validate function
Kubernetes-commit: c41fb9e3bfe5d13347a46d6b08f1b2a8347b1bc8
This commit is contained in:
parent
f3e568c411
commit
ccd9dd871c
|
@ -73,16 +73,7 @@ func NewCmdConfigGetContexts(streams genericclioptions.IOStreams, configAccess c
|
||||||
Long: getContextsLong,
|
Long: getContextsLong,
|
||||||
Example: getContextsExample,
|
Example: getContextsExample,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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")
|
cmdutil.CheckErr(options.Validate(cmd))
|
||||||
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.Complete(cmd, args))
|
cmdutil.CheckErr(options.Complete(cmd, args))
|
||||||
cmdutil.CheckErr(options.RunGetContexts())
|
cmdutil.CheckErr(options.RunGetContexts())
|
||||||
},
|
},
|
||||||
|
@ -108,6 +99,21 @@ func (o *GetContextsOptions) Complete(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
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.
|
// RunGetContexts implements all the necessary functionality for context retrieval.
|
||||||
func (o GetContextsOptions) RunGetContexts() error {
|
func (o GetContextsOptions) RunGetContexts() error {
|
||||||
config, err := o.configAccess.GetStartingConfig()
|
config, err := o.configAccess.GetStartingConfig()
|
||||||
|
|
Loading…
Reference in New Issue