Merge pull request #54142 from neolit123/cmdutil-01

Automatic merge from submit-queue (batch tested with PRs 53809, 54244, 54142). 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>.

kubeadm/cmdutil.go: improve ValidateExactArgNumber()

**What this PR does / why we need it**:
This patch makes small changes in
ValidateExactArgNumber():

- Use a variable for the length of supported arguments
- Return an error early if the number of valid arguments
exceeds the number of supported arguments

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

**Special notes for your reviewer**:
none

**Release note**:

```release-note
NONE
```

Lubomir (VMware)
pull/6/head
Kubernetes Submit Queue 2017-10-19 11:50:05 -07:00 committed by GitHub
commit 7f49a2ccfc
1 changed files with 6 additions and 4 deletions

View File

@ -39,19 +39,21 @@ func SubCmdRunE(name string) func(*cobra.Command, []string) error {
// ValidateExactArgNumber validates that the required top-level arguments are specified
func ValidateExactArgNumber(args []string, supportedArgs []string) error {
lenSupported := len(supportedArgs)
validArgs := 0
// Disregard possible "" arguments; they are invalid
for _, arg := range args {
if len(arg) > 0 {
validArgs++
}
// break early for too many arguments
if validArgs > lenSupported {
return fmt.Errorf("too many arguments. Required arguments: %v", supportedArgs)
}
}
if validArgs < len(supportedArgs) {
if validArgs < lenSupported {
return fmt.Errorf("missing one or more required arguments. Required arguments: %v", supportedArgs)
}
if validArgs > len(supportedArgs) {
return fmt.Errorf("too many arguments, only %d argument(s) supported: %v", validArgs, supportedArgs)
}
return nil
}