From 7d311d3636c30c68c3f883be1021d7ba3b8b2bcb 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 --- ...1115--cli--fix-stack-selection-prompt.yaml | 4 ++++ pkg/cmd/pulumi/util.go | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 changelog/pending/20221115--cli--fix-stack-selection-prompt.yaml diff --git a/changelog/pending/20221115--cli--fix-stack-selection-prompt.yaml b/changelog/pending/20221115--cli--fix-stack-selection-prompt.yaml new file mode 100644 index 000000000000..9765f301ab00 --- /dev/null +++ b/changelog/pending/20221115--cli--fix-stack-selection-prompt.yaml @@ -0,0 +1,4 @@ +changes: +- type: fix + scope: cli + description: Fix stack selection prompt. diff --git a/pkg/cmd/pulumi/util.go b/pkg/cmd/pulumi/util.go index e10d8b1581e1..7b20933e440e 100644 --- a/pkg/cmd/pulumi/util.go +++ b/pkg/cmd/pulumi/util.go @@ -347,23 +347,28 @@ 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 +383,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) }