Merge pull request #42999 from shiywang/fix-return0

Automatic merge from submit-queue (batch tested with PRs 43450, 42999, 43968)

fix kubectl config return 0 on error

Fixes https://github.com/kubernetes/kubernetes/issues/42852
cc @kubernetes/sig-cli-bugs
@ymqytw ptal
This commit is contained in:
Kubernetes Submit Queue 2017-04-03 09:48:23 -07:00 committed by GitHub
commit 1df2363093
7 changed files with 29 additions and 46 deletions

View File

@ -105,11 +105,11 @@ func newCmdConfigSetAuthInfo(out io.Writer, options *createAuthInfoOptions) *cob
Long: create_authinfo_long, Long: create_authinfo_long,
Example: create_authinfo_example, Example: create_authinfo_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd, out) { err := options.complete(cmd, out)
if err != nil {
cmd.Help() cmd.Help()
return cmdutil.CheckErr(err)
} }
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "User %q set.\n", options.name) fmt.Fprintf(out, "User %q set.\n", options.name)
}, },
@ -238,30 +238,28 @@ func (o *createAuthInfoOptions) modifyAuthInfo(existingAuthInfo clientcmdapi.Aut
return modifiedAuthInfo return modifiedAuthInfo
} }
func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) bool { func (o *createAuthInfoOptions) complete(cmd *cobra.Command, out io.Writer) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) != 1 { if len(args) != 1 {
return false return fmt.Errorf("Unexpected args: %v", args)
} }
authProviderArgs, err := cmd.Flags().GetStringSlice(flagAuthProviderArg) authProviderArgs, err := cmd.Flags().GetStringSlice(flagAuthProviderArg)
if err != nil { if err != nil {
fmt.Fprintf(out, "Error: %s\n", err) return fmt.Errorf("Error: %s\n", err)
return false
} }
if len(authProviderArgs) > 0 { if len(authProviderArgs) > 0 {
newPairs, removePairs, err := cmdutil.ParsePairs(authProviderArgs, flagAuthProviderArg, true) newPairs, removePairs, err := cmdutil.ParsePairs(authProviderArgs, flagAuthProviderArg, true)
if err != nil { if err != nil {
fmt.Fprintf(out, "Error: %s\n", err) return fmt.Errorf("Error: %s\n", err)
return false
} }
o.authProviderArgs = newPairs o.authProviderArgs = newPairs
o.authProviderArgsToRemove = removePairs o.authProviderArgsToRemove = removePairs
} }
o.name = args[0] o.name = args[0]
return true return nil
} }
func (o createAuthInfoOptions) validate() error { func (o createAuthInfoOptions) validate() error {

View File

@ -160,7 +160,7 @@ func TestCreateAuthInfoOptions(t *testing.T) {
continue continue
} }
if !opts.complete(cmd, buff) { if err := opts.complete(cmd, buff); err != nil {
if !test.wantCompleteErr { if !test.wantCompleteErr {
t.Errorf("case %d: complete() error for flags %q: %s", i, test.flags, buff) t.Errorf("case %d: complete() error for flags %q: %s", i, test.flags, buff)
} }

View File

@ -69,10 +69,7 @@ func NewCmdConfigSetCluster(out io.Writer, configAccess clientcmd.ConfigAccess)
Long: create_cluster_long, Long: create_cluster_long,
Example: create_cluster_example, Example: create_cluster_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) { cmdutil.CheckErr(options.complete(cmd))
return
}
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "Cluster %q set.\n", options.name) fmt.Fprintf(out, "Cluster %q set.\n", options.name)
}, },
@ -155,15 +152,15 @@ func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluste
return modifiedCluster return modifiedCluster
} }
func (o *createClusterOptions) complete(cmd *cobra.Command) bool { func (o *createClusterOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) != 1 { if len(args) != 1 {
cmd.Help() cmd.Help()
return false return fmt.Errorf("Unexpected args: %v", args)
} }
o.name = args[0] o.name = args[0]
return true return nil
} }
func (o createClusterOptions) validate() error { func (o createClusterOptions) validate() error {

View File

@ -59,10 +59,7 @@ func NewCmdConfigSetContext(out io.Writer, configAccess clientcmd.ConfigAccess)
Long: create_context_long, Long: create_context_long,
Example: create_context_example, Example: create_context_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) { cmdutil.CheckErr(options.complete(cmd))
return
}
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "Context %q set.\n", options.name) fmt.Fprintf(out, "Context %q set.\n", options.name)
}, },
@ -116,15 +113,15 @@ func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Contex
return modifiedContext return modifiedContext
} }
func (o *createContextOptions) complete(cmd *cobra.Command) bool { func (o *createContextOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args() args := cmd.Flags().Args()
if len(args) != 1 { if len(args) != 1 {
cmd.Help() cmd.Help()
return false return fmt.Errorf("Unexpected args: %v", args)
} }
o.name = args[0] o.name = args[0]
return true return nil
} }
func (o createContextOptions) validate() error { func (o createContextOptions) validate() error {

View File

@ -60,10 +60,7 @@ func NewCmdConfigSet(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.
Short: i18n.T("Sets an individual value in a kubeconfig file"), Short: i18n.T("Sets an individual value in a kubeconfig file"),
Long: set_long, Long: set_long,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) { cmdutil.CheckErr(options.complete(cmd))
return
}
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "Property %q set.\n", options.propertyName) fmt.Fprintf(out, "Property %q set.\n", options.propertyName)
}, },
@ -106,16 +103,16 @@ func (o setOptions) run() error {
return nil return nil
} }
func (o *setOptions) complete(cmd *cobra.Command) bool { func (o *setOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args() endingArgs := cmd.Flags().Args()
if len(endingArgs) != 2 { if len(endingArgs) != 2 {
cmd.Help() cmd.Help()
return false return fmt.Errorf("Unexpected args: %v", endingArgs)
} }
o.propertyValue = endingArgs[1] o.propertyValue = endingArgs[1]
o.propertyName = endingArgs[0] o.propertyName = endingArgs[0]
return true return nil
} }
func (o setOptions) validate() error { func (o setOptions) validate() error {

View File

@ -48,10 +48,7 @@ func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobr
Short: i18n.T("Unsets an individual value in a kubeconfig file"), Short: i18n.T("Unsets an individual value in a kubeconfig file"),
Long: unset_long, Long: unset_long,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) { cmdutil.CheckErr(options.complete(cmd))
return
}
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "Property %q unset.\n", options.propertyName) fmt.Fprintf(out, "Property %q unset.\n", options.propertyName)
}, },
@ -87,15 +84,15 @@ func (o unsetOptions) run() error {
return nil return nil
} }
func (o *unsetOptions) complete(cmd *cobra.Command) bool { func (o *unsetOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args() endingArgs := cmd.Flags().Args()
if len(endingArgs) != 1 { if len(endingArgs) != 1 {
cmd.Help() cmd.Help()
return false return fmt.Errorf("Unexpected args: %v", endingArgs)
} }
o.propertyName = endingArgs[0] o.propertyName = endingArgs[0]
return true return nil
} }
func (o unsetOptions) validate() error { func (o unsetOptions) validate() error {

View File

@ -50,10 +50,7 @@ func NewCmdConfigUseContext(out io.Writer, configAccess clientcmd.ConfigAccess)
Long: `Sets the current-context in a kubeconfig file`, Long: `Sets the current-context in a kubeconfig file`,
Example: use_context_example, Example: use_context_example,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !options.complete(cmd) { cmdutil.CheckErr(options.complete(cmd))
return
}
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run())
fmt.Fprintf(out, "Switched to context %q.\n", options.contextName) fmt.Fprintf(out, "Switched to context %q.\n", options.contextName)
}, },
@ -82,15 +79,15 @@ func (o useContextOptions) run() error {
return nil return nil
} }
func (o *useContextOptions) complete(cmd *cobra.Command) bool { func (o *useContextOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args() endingArgs := cmd.Flags().Args()
if len(endingArgs) != 1 { if len(endingArgs) != 1 {
cmd.Help() cmd.Help()
return false return fmt.Errorf("Unexpected args: %v", endingArgs)
} }
o.contextName = endingArgs[0] o.contextName = endingArgs[0]
return true return nil
} }
func (o useContextOptions) validate(config *clientcmdapi.Config) error { func (o useContextOptions) validate(config *clientcmdapi.Config) error {