pulumi/cmd/stack_select.go
Chris Smith c286712d28
Remove args we can now get from the repository and package (#501)
This PR removes three command line parameters from Cloud-enabled Pulumi commands (`update` and `stack init`). Previously we required users to pass in `--organization`, `--repository`, and `--project`. But with the recent "Pulumi repository" changes, we can now get that from the Pulumi workspace. And the project name from the `Pulumi.yaml`.

This PR also fixes a bugs that block the Cloud-enabled CLI path: `update` was getting the stack name via `explicitOrCurrent`, but that fails if the current stack (e.g. the one just initialized in the cloud) doesn't exist on the local disk.

As for better handling of "current stack" and and Cloud-enabled commands, https://github.com/pulumi/pulumi/pull/493 and the PR to enable `stack select`, `stack rm`, and `stack ls` do a better job of handling situations like this.
2017-10-30 17:47:12 -07:00

51 lines
1.3 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newStackSelectCmd() *cobra.Command {
return &cobra.Command{
Use: "select [<stack>]",
Short: "Switch the current workspace to the given stack",
Long: "Switch the current workspace to the given stack. This allows you to use\n" +
"other commands like `config`, `preview`, and `push` without needing to specify the\n" +
"stack name each and every time.\n" +
"\n" +
"If no <stack> argument is supplied, the current stack is printed.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Read in the name of the stack to switch to.
if len(args) == 0 {
name, err := getCurrentStack()
if err != nil {
return err
}
fmt.Printf("%v\n", name)
return nil
}
allStacks, err := getStacks()
if err != nil {
return err
}
// Confirm the stack name is valid.
selectedStack := tokens.QName(args[0])
for _, stack := range allStacks {
if stack == selectedStack {
return setCurrentStack(selectedStack)
}
}
return fmt.Errorf("no stack with name '%v' found", selectedStack)
}),
}
}