pulumi/cmd/stack_init.go
Matt Ellis 0d47915beb Provide a better error message for stack init when not logged in
`pulumi stack init` defaults to trying to create a stack in the Pulumi
Cloud. If you are not logged in, it prints an error telling you to log
in.

With this change, the error message also points out that you can pass
`--local` to `pulumi stack init` to create the stack locally.
2018-01-19 09:45:54 -08:00

71 lines
2.5 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/backend/state"
"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 ppc string
cmd := &cobra.Command{
Use: "init <stack-name>",
Args: cmdutil.SpecificArgs([]string{"stack-name"}),
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 {
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")
}
b = local.New(cmdutil.Diag())
} else {
// If no cloud URL override was given, fall back to the default.
if cloudURL == "" {
cloudURL = cloud.DefaultURL()
}
// Check to see if the user is logged in, if they are not, fail with a nicer message
if creds, err := workspace.GetAccessToken(cloudURL); err != nil || creds == "" {
return errors.New("you must be logged in to create stacks in the Pulumi Cloud. Run " +
"`pulumi login` to log in or pass `--local` to create the stack locally.")
}
b = cloud.New(cmdutil.Diag(), cloudURL)
opts = cloud.CreateStackOptions{CloudName: ppc}
}
stackName := tokens.QName(args[0])
err := b.CreateStack(stackName, opts)
if err != nil {
return errors.Wrapf(err, "could not create stack")
}
return state.SetCurrentStack(stackName)
}),
}
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().StringVarP(
&ppc, "ppc", "p", "", "A Pulumi Private Cloud (PPC) name to initialize this stack in (if not --local)")
return cmd
}