From 5b0a586602ac0f7b11750959d747ad3dbebcfad8 Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Tue, 15 Nov 2022 11:29:56 +0000 Subject: [PATCH] Fix default answer for AskOne prompt Fixes https://github.com/pulumi/pulumi/issues/11334 --- pkg/cmd/pulumi/util.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/pulumi/util.go b/pkg/cmd/pulumi/util.go index e10d8b1581e1..e4e3493a6fae 100644 --- a/pkg/cmd/pulumi/util.go +++ b/pkg/cmd/pulumi/util.go @@ -347,23 +347,27 @@ func chooseStack(ctx context.Context, } sort.Strings(options) + // If a stack is already selected, make that the default. + var defaultOption string + currStack, currErr := state.CurrentStack(ctx, b) + contract.IgnoreError(currErr) + if currStack != nil { + defaultOption = currStack.Ref().String() + } + // If we are offering to create a new stack, add that to the end of the list. const newOption = "" if offerNew { options = append(options, newOption) + // If we're offering the option to make a new stack AND we don't have a default current stack then make the new option the default + if defaultOption == "" { + defaultOption = newOption + } } else if len(options) == 0 { // If no options are available, we can't offer a choice! return nil, errors.New("this command requires a stack, but there are none") } - // If a stack is already selected, make that the default. - var current string - currStack, currErr := state.CurrentStack(ctx, b) - contract.IgnoreError(currErr) - if currStack != nil { - current = currStack.Ref().String() - } - // Customize the prompt a little bit (and disable color since it doesn't match our scheme). surveycore.DisableColor = true message := "\rPlease choose a stack" @@ -378,7 +382,7 @@ func chooseStack(ctx context.Context, if err = survey.AskOne(&survey.Select{ Message: message, Options: options, - Default: current, + Default: defaultOption, }, &option, surveyIcons(opts.Color)); err != nil { return nil, errors.New(chooseStackErr) }