kubeadm/cmdutil.go: minor improvements

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

Signed-off-by: Lubomir I. Ivanov <lubomirivanov@vmware.com>
This commit is contained in:
Lubomir I. Ivanov 2017-10-18 14:43:50 +03:00
parent be8f1cea80
commit 6f35f1cbf8

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
}