Skip to content

Commit

Permalink
fix: Panic when passing too many args
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasK33 committed Oct 15, 2024
1 parent d524065 commit 72342df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
9 changes: 7 additions & 2 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/vclusterctl/cmd/platform/add/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/vclusterctl/cmd/platform/create/vcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/cli/util/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 72342df

Please sign in to comment.