Skip to content

Commit

Permalink
Fix default answer for AskOne prompt
Browse files Browse the repository at this point in the history
Fixes #11334
  • Loading branch information
Frassle committed Nov 15, 2022
1 parent f6d09b8 commit 5b0a586
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pkg/cmd/pulumi/util.go
Expand Up @@ -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 = "<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 +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)
}
Expand Down

0 comments on commit 5b0a586

Please sign in to comment.