mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
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:
parent
be8f1cea80
commit
6f35f1cbf8
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user