pulumi/cmd/stack_init.go
Justin Van Patten ac58f151b4
Change default of where stack is created (#971)
If currently logged in, `stack init` creates a managed stack. Otherwise, it creates a local
stack. This avoids the need to specify `--local` when not using the service.

As today, `--local` can be passed, which will create a local stack regardless of being logged
in or not.

A new flag, `--remote`, has been added, which can be passed to indicate a managed stack,
used to force an error if not logged into the service.
2018-02-26 11:00:16 -08:00

88 lines
2.8 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/backend/cloud"
"github.com/pulumi/pulumi/pkg/backend/local"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
func newStackInitCmd() *cobra.Command {
var cloudURL string
var localBackend bool
var remoteBackend bool
var ppc string
cmd := &cobra.Command{
Use: "init <stack-name>",
Args: cmdutil.MaximumNArgs(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 {
cloudURL = cloud.ValueOrDefaultURL(cloudURL)
var b backend.Backend
var opts interface{}
if localBackend {
if ppc != "" {
return errors.New("cannot pass both --local and --ppc; PPCs only available in cloud mode")
}
if remoteBackend {
return errors.New("cannot pass both --local and --remote")
}
b = local.New(cmdutil.Diag())
} else if isLoggedIn(cloudURL) {
b = cloud.New(cmdutil.Diag(), cloudURL)
opts = cloud.CreateStackOptions{CloudName: ppc}
} else {
// If the user is not logged in and --remote was passed, fail.
if remoteBackend {
return errors.New("you must be logged in to create stacks in the Pulumi Cloud. Run " +
"`pulumi login` to log in.")
}
b = local.New(cmdutil.Diag())
}
var stackName tokens.QName
if len(args) > 0 {
stackName = tokens.QName(args[0])
} else if cmdutil.Interactive() {
name, err := cmdutil.ReadConsole("Enter a stack name")
if err != nil {
return err
}
stackName = tokens.QName(name)
}
if stackName == "" {
return errors.New("missing stack name")
}
_, err := createStack(b, stackName, opts)
return err
}),
}
cmd.PersistentFlags().StringVarP(
&cloudURL, "cloud-url", "c", "", "A URL for the Pulumi Cloud in which to initialize this stack")
cmd.PersistentFlags().BoolVarP(
&localBackend, "local", "l", false, "Initialize this stack locally instead of in the Pulumi Cloud")
cmd.PersistentFlags().BoolVarP(
&remoteBackend, "remote", "r", false, "Initialize this stack in the Pulumi Cloud instead of locally")
cmd.PersistentFlags().StringVarP(
&ppc, "ppc", "p", "", "A Pulumi Private Cloud (PPC) name to initialize this stack in (if not --local)")
return cmd
}
func isLoggedIn(cloudURL string) bool {
creds, err := workspace.GetAccessToken(cloudURL)
return err == nil && creds != ""
}