Merge pull request #52019 from zjj2wry/kubectl-config-use

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>..

fix issue(11233)enhance kubectl config command

**What this PR does / why we need it**:
Fixes #11233

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-09-23 17:20:48 -07:00 committed by GitHub
commit 2f64309d0d
7 changed files with 16 additions and 22 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package config
import (
"fmt"
"io"
"path"
"strconv"
@ -81,3 +82,9 @@ func toBool(propertyValue string) (bool, error) {
return boolValue, nil
}
func helpErrorf(cmd *cobra.Command, format string, args ...interface{}) error {
cmd.Help()
msg := fmt.Sprintf(format, args...)
return fmt.Errorf("%s\n", msg)
}

View File

@ -150,8 +150,7 @@ func (o *createClusterOptions) modifyCluster(existingCluster clientcmdapi.Cluste
func (o *createClusterOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args()
if len(args) != 1 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", args)
return helpErrorf(cmd, "Unexpected args: %v", args)
}
o.name = args[0]

View File

@ -121,8 +121,7 @@ func (o *createContextOptions) modifyContext(existingContext clientcmdapi.Contex
func (o *createContextOptions) complete(cmd *cobra.Command) error {
args := cmd.Flags().Args()
if len(args) != 1 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", args)
return helpErrorf(cmd, "Unexpected args: %v", args)
}
o.name = args[0]

View File

@ -83,8 +83,7 @@ func NewCmdConfigRenameContext(out io.Writer, configAccess clientcmd.ConfigAcces
// Complete assigns RenameContextOptions from the args.
func (o *RenameContextOptions) Complete(cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) != 2 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", args)
return helpErrorf(cmd, "Unexpected args: %v", args)
}
o.contextName = args[0]

View File

@ -106,8 +106,7 @@ func (o setOptions) run() error {
func (o *setOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args()
if len(endingArgs) != 2 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", endingArgs)
return helpErrorf(cmd, "Unexpected args: %v", endingArgs)
}
o.propertyValue = endingArgs[1]

View File

@ -77,18 +77,13 @@ func (o unsetOptions) run() error {
return err
}
if err := clientcmd.ModifyConfig(o.configAccess, *config, false); err != nil {
return err
}
return nil
return clientcmd.ModifyConfig(o.configAccess, *config, false)
}
func (o *unsetOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args()
if len(endingArgs) != 1 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", endingArgs)
return helpErrorf(cmd, "Unexpected args: %v", endingArgs)
}
o.propertyName = endingArgs[0]

View File

@ -47,6 +47,7 @@ func NewCmdConfigUseContext(out io.Writer, configAccess clientcmd.ConfigAccess)
cmd := &cobra.Command{
Use: "use-context CONTEXT_NAME",
Short: i18n.T("Sets the current-context in a kubeconfig file"),
Aliases: []string{"use"},
Long: `Sets the current-context in a kubeconfig file`,
Example: use_context_example,
Run: func(cmd *cobra.Command, args []string) {
@ -72,18 +73,13 @@ func (o useContextOptions) run() error {
config.CurrentContext = o.contextName
if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {
return err
}
return nil
return clientcmd.ModifyConfig(o.configAccess, *config, true)
}
func (o *useContextOptions) complete(cmd *cobra.Command) error {
endingArgs := cmd.Flags().Args()
if len(endingArgs) != 1 {
cmd.Help()
return fmt.Errorf("Unexpected args: %v", endingArgs)
return helpErrorf(cmd, "Unexpected args: %v", endingArgs)
}
o.contextName = endingArgs[0]