pulumi/cmd/stack_init.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

97 lines
2.7 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"github.com/pulumi/pulumi/pkg/apitype"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newStackInitCmd() *cobra.Command {
if usePulumiCloudCommands() {
return newCloudStackInitCmd()
}
return newFAFStackInitCmd()
}
func newFAFStackInitCmd() *cobra.Command {
return &cobra.Command{
Use: "init <stack>",
Args: cobra.ExactArgs(1),
Short: "Create an empty stack with the given name, ready for updates",
Long: "Create an empty stack with the given name, ready for updates\n" +
"\n" +
"This command creates an empty stack with the given name. It has no resources,\n" +
"but afterwards it can become the target of a deployment using the `update` command.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
stackName := tokens.QName(args[0])
if _, _, _, err := getStack(stackName); err == nil {
return fmt.Errorf("stack '%v' already exists", stackName)
}
err := saveStack(stackName, nil, nil)
if err != nil {
return err
}
return setCurrentStack(stackName)
}),
}
}
func newCloudStackInitCmd() *cobra.Command {
var cloud string
cmd := &cobra.Command{
Use: "init <stack>",
Args: cobra.ExactArgs(1),
Short: "Create an empty stack with the given name, ready for updates",
Long: "Create an empty stack with the given name, ready for updates\n" +
"\n" +
"This command creates an empty stack with the given name. It has no resources,\n" +
"but afterwards it can become the target of a deployment using the `update` command.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Look up the owner, repository, and project from the workspace and nearest package.
w, err := newWorkspace()
if err != nil {
return err
}
projID, err := getCloudProjectIdentifier(w)
if err != nil {
return err
}
stackName := args[0]
createStackReq := apitype.CreateStackRequest{
CloudName: cloud,
StackName: stackName,
}
var createStackResp apitype.CreateStackResponse
path := fmt.Sprintf("/orgs/%s/programs/%s/%s/stacks", projID.Owner, projID.Repository, projID.Project)
if err := pulumiRESTCall("POST", path, &createStackReq, &createStackResp); err != nil {
return err
}
fmt.Printf("Created Stack '%s' hosted in Cloud '%s'\n", stackName, createStackResp.CloudName)
stackQName := tokens.QName(stackName)
return setCurrentStack(stackQName)
}),
}
// If not set will use the "default" cloud for the organization.
cmd.PersistentFlags().StringVarP(
&cloud, "cloud", "c", "",
"Target cloud")
return cmd
}