pulumi/cmd/stack_init.go
Joe Duffy 36ab8f0087
Make config a little less error prone
As articulated in #714, the way config defaults to workspace-local
configuration is a bit error prone, especially now with the cloud
workflow being the default.  This change implements several improvements:

* First, --save defaults to true, so that configuration changes will
  persist into your project file.  If you want the old local workspace
  behavior, you can specify --save=false.

* Second, the order in which we applied configuration was a little
  strange, because workspace settings overwrote project settings.
  The order is changed now so that we take most specific over least
  specific configuration.  Per-stack is considered more specific
  than global and project settings are considered more specific
  than workspace.

* We now warn anytime workspace local configuration is used.  This
  is a developer scenario and can have subtle effects.  It is simply
  not safe to use in a team environment.  In fact, I lost an arm
  this morning due to workspace config... and that's why you always
  issue warnings for unsafe things.
2017-12-13 10:46:54 -08:00

63 lines
2.1 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"
)
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()
}
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
}