/
stack_select.go
116 lines (102 loc) 路 3.63 KB
/
stack_select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/backend/state"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
// newStackSelectCmd handles both the "local" and "cloud" scenarios in its implementation.
func newStackSelectCmd() *cobra.Command {
var stack string
var secretsProvider string
var create bool
cmd := &cobra.Command{
Use: "select [<stack>]",
Short: "Switch the current workspace to the given stack",
Long: "Switch the current workspace to the given stack.\n" +
"\n" +
"Selecting a stack allows you to use commands like `config`, `preview`, and `update`\n" +
"without needing to type the stack name each time.\n" +
"\n" +
"If no <stack> argument is supplied, you will be prompted to select one interactively.\n" +
"If provided stack name is not found you may pass the --create flag to create and select it",
Args: cmdutil.MaximumNArgs(1),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
opts := display.Options{
Color: cmdutil.GetGlobalColorization(),
}
// Try to read the current project
project, root, err := readProject()
if err != nil && !errors.Is(err, workspace.ErrProjectNotFound) {
return err
}
b, err := currentBackend(ctx, project, opts)
if err != nil {
return err
}
if len(args) > 0 {
if stack != "" {
return errors.New("only one of --stack or argument stack name may be specified, not both")
}
stack = args[0]
}
if stack != "" {
// A stack was given, ask the backend about it.
stackRef, stackErr := b.ParseStackReference(stack)
if stackErr != nil {
return stackErr
}
s, stackErr := b.GetStack(ctx, stackRef)
if stackErr != nil {
return stackErr
} else if s != nil {
return state.SetCurrentStack(stackRef.String())
}
// If create flag was passed and stack was not found, create it and select it.
if create && stack != "" {
s, err := stackInit(ctx, b, stack, root, false, secretsProvider)
if err != nil {
return err
}
return state.SetCurrentStack(s.Ref().String())
}
return fmt.Errorf("no stack named '%s' found", stackRef)
}
// If no stack was given, prompt the user to select a name from the available ones.
stack, err := chooseStack(ctx, b, stackOfferNew|stackSetCurrent, opts)
if err != nil {
return err
}
contract.Assertf(stack != nil, "must select a stack")
return state.SetCurrentStack(stack.Ref().String())
}),
}
cmd.PersistentFlags().StringVarP(
&stack, "stack", "s", "",
"The name of the stack to select")
cmd.PersistentFlags().BoolVarP(
&create, "create", "c", false,
"If selected stack does not exist, create it")
cmd.PersistentFlags().StringVar(
&secretsProvider, "secrets-provider", "default",
"Use with --create flag, "+possibleSecretsProviderChoices)
return cmd
}