Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Panic when passing too many args #2227

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading