diff --git a/cmd/vclusterctl/cmd/create.go b/cmd/vclusterctl/cmd/create.go index dc39e36c9..f7e8972da 100644 --- a/cmd/vclusterctl/cmd/create.go +++ b/cmd/vclusterctl/cmd/create.go @@ -47,8 +47,13 @@ vcluster create test --namespace test `, RunE: func(cobraCmd *cobra.Command, args []string) error { newArgs, err := util.PromptForArgs(cmd.log, args, "vcluster name") - if err != nil && errors.Is(err, util.ErrNonInteractive) { - if err := util.VClusterNameOnlyValidator(cobraCmd, args); err != nil { + if err != nil { + switch { + case errors.Is(err, util.ErrNonInteractive): + if err := util.VClusterNameOnlyValidator(cobraCmd, args); err != nil { + return err + } + default: return err } } diff --git a/cmd/vclusterctl/cmd/platform/add/cluster.go b/cmd/vclusterctl/cmd/platform/add/cluster.go index bfd0d28f8..bbac2f9ba 100644 --- a/cmd/vclusterctl/cmd/platform/add/cluster.go +++ b/cmd/vclusterctl/cmd/platform/add/cluster.go @@ -63,8 +63,13 @@ vcluster platform add cluster my-cluster `, RunE: func(cobraCmd *cobra.Command, args []string) error { newArgs, err := util.PromptForArgs(cmd.Log, args, "cluster name") - if err != nil && errors.Is(err, util.ErrNonInteractive) { - if err := cobra.ExactArgs(1)(cobraCmd, args); err != nil { + if err != nil { + switch { + case errors.Is(err, util.ErrNonInteractive): + if err := cobra.ExactArgs(1)(cobraCmd, args); err != nil { + return err + } + default: return err } } diff --git a/cmd/vclusterctl/cmd/platform/create/vcluster.go b/cmd/vclusterctl/cmd/platform/create/vcluster.go index 9445839ba..6a8fa6ae0 100644 --- a/cmd/vclusterctl/cmd/platform/create/vcluster.go +++ b/cmd/vclusterctl/cmd/platform/create/vcluster.go @@ -42,8 +42,13 @@ vcluster platform create vcluster test --namespace test `, RunE: func(cobraCmd *cobra.Command, args []string) error { newArgs, err := util.PromptForArgs(cmd.log, args, "vcluster name") - if err != nil && errors.Is(err, util.ErrNonInteractive) { - if err := util.VClusterNameOnlyValidator(cobraCmd, args); err != nil { + if err != nil { + switch { + case errors.Is(err, util.ErrNonInteractive): + if err := util.VClusterNameOnlyValidator(cobraCmd, args); err != nil { + return err + } + default: return err } } diff --git a/pkg/cli/util/args.go b/pkg/cli/util/args.go index 16acc0940..dec8756b7 100644 --- a/pkg/cli/util/args.go +++ b/pkg/cli/util/args.go @@ -18,8 +18,11 @@ var ( VClusterNameOnlyUseLine string VClusterNameOnlyValidator cobra.PositionalArgs +) - ErrNonInteractive = errors.New("terminal is not interactive") +var ( + ErrNonInteractive = errors.New("terminal is not interactive") + ErrTooManyArguments = errors.New("too many arguments specified") ) func init() { @@ -81,6 +84,9 @@ func PromptForArgs(l log.Logger, args []string, argNames ...string) ([]string, e if !terminal.IsTerminalIn { return args, ErrNonInteractive } + if len(args) > len(argNames) { + return args, ErrTooManyArguments + } if len(args) == len(argNames) { return args, nil @@ -90,7 +96,6 @@ func PromptForArgs(l log.Logger, args []string, argNames ...string) ([]string, e answer, err := l.Question(&survey.QuestionOptions{ Question: fmt.Sprintf("Please specify %s", argNames[i]), }) - if err != nil { return args, err }