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 default answer for AskOne prompt #11354

Merged
merged 1 commit into from Nov 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: cli
description: Fix stack selection prompt.
23 changes: 14 additions & 9 deletions pkg/cmd/pulumi/util.go
Expand Up @@ -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 = "<create a new stack>"
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"
Expand All @@ -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)
}
Expand Down