From 6f35f1cbf805ed1b8d858f1066c8a730c16582d4 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Wed, 18 Oct 2017 14:43:50 +0300 Subject: [PATCH] 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 --- cmd/kubeadm/app/cmd/util/cmdutil.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/kubeadm/app/cmd/util/cmdutil.go b/cmd/kubeadm/app/cmd/util/cmdutil.go index 3c0e7d65b10..21386775103 100644 --- a/cmd/kubeadm/app/cmd/util/cmdutil.go +++ b/cmd/kubeadm/app/cmd/util/cmdutil.go @@ -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 }