Fix panic on stack select (#3687)
This commit is contained in:
parent
d66f081e27
commit
be840413a1
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/pulumi/pulumi/pkg/backend/display"
|
||||
"github.com/pulumi/pulumi/pkg/backend/state"
|
||||
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
||||
"github.com/pulumi/pulumi/pkg/util/contract"
|
||||
)
|
||||
|
||||
// newStackSelectCmd handles both the "local" and "cloud" scenarios in its implementation.
|
||||
|
@ -55,7 +56,7 @@ func newStackSelectCmd() *cobra.Command {
|
|||
}
|
||||
|
||||
if stack != "" {
|
||||
// A stack was given, ask the backend about it
|
||||
// A stack was given, ask the backend about it.
|
||||
stackRef, stackErr := b.ParseStackReference(stack)
|
||||
if stackErr != nil {
|
||||
return stackErr
|
||||
|
@ -76,6 +77,8 @@ func newStackSelectCmd() *cobra.Command {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
contract.Assert(stack != nil)
|
||||
return state.SetCurrentStack(stack.Ref().String())
|
||||
|
||||
}),
|
||||
|
|
12
cmd/util.go
12
cmd/util.go
|
@ -229,6 +229,8 @@ func requireCurrentStack(offerNew bool, opts display.Options, setCurrent bool) (
|
|||
// true, then the option to create an entirely new stack is provided and will create one as desired.
|
||||
func chooseStack(
|
||||
b backend.Backend, offerNew bool, opts display.Options, setCurrent bool) (backend.Stack, error) {
|
||||
ctx := commandContext()
|
||||
|
||||
// Prepare our error in case we need to issue it. Bail early if we're not interactive.
|
||||
var chooseStackErr string
|
||||
if offerNew {
|
||||
|
@ -247,7 +249,7 @@ func chooseStack(
|
|||
|
||||
// List stacks as available options.
|
||||
project := string(proj.Name)
|
||||
summaries, err := b.ListStacks(commandContext(), backend.ListStacksFilter{Project: &project})
|
||||
summaries, err := b.ListStacks(ctx, backend.ListStacksFilter{Project: &project})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "could not query backend for stacks")
|
||||
}
|
||||
|
@ -270,7 +272,7 @@ func chooseStack(
|
|||
|
||||
// If a stack is already selected, make that the default.
|
||||
var current string
|
||||
currStack, currErr := state.CurrentStack(commandContext(), b)
|
||||
currStack, currErr := state.CurrentStack(ctx, b)
|
||||
contract.IgnoreError(currErr)
|
||||
if currStack != nil {
|
||||
current = currStack.Ref().String()
|
||||
|
@ -321,10 +323,14 @@ func chooseStack(
|
|||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parsing selected stack")
|
||||
}
|
||||
stack, err := b.GetStack(commandContext(), stackRef)
|
||||
// GetStack may return (nil, nil) if the stack isn't found.
|
||||
stack, err := b.GetStack(ctx, stackRef)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getting selected stack")
|
||||
}
|
||||
if stack == nil {
|
||||
return nil, errors.Errorf("no stack named '%s' found", stackRef)
|
||||
}
|
||||
|
||||
// If setCurrent is true, we'll persist this choice so it'll be used for future CLI operations.
|
||||
if setCurrent {
|
||||
|
|
Loading…
Reference in a new issue