pulumi/cmd/stack_init.go
Joe Duffy 776a76dffd
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.

This includes:

* If `pulumi stack select` is run by itself, use an interactive
  CLI menu to let the user select an existing stack, or choose to
  create a new one.  This looks as follows

      $ pulumi stack select
      Please choose a stack, or choose to create a new one:
        abcdef
        babblabblabble
      > currentlyselected
        defcon
        <create a new stack>

  and is navigated in the usual way (key up, down, enter).

* If a stack name is passed that does not exist, prompt the user
  to ask whether s/he wants to create one on-demand.  This hooks
  interesting moments in time, like `pulumi stack select foo`,
  and cuts down on the need to run additional commands.

* If a current stack is required, but none is currently selected,
  then pop the same interactive menu shown above to select one.
  Depending on the command being run, we may or may not show the
  option to create a new stack (e.g., that doesn't make much sense
  when you're running `pulumi destroy`, but might when you're
  running `pulumi stack`).  This again lets you do with a single
  command what would have otherwise entailed an error with multiple
  commands to recover from it.

* If you run `pulumi stack init` without any additional arguments,
  we interactively prompt for the stack name.  Before, we would
  error and you'd then need to run `pulumi stack init <name>`.

* Colorize some things nicely; for example, now all prompts will
  by default become bright white.
2018-02-16 15:03:54 -08:00

80 lines
2.6 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 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 {
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}
}
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().StringVarP(
&ppc, "ppc", "p", "", "A Pulumi Private Cloud (PPC) name to initialize this stack in (if not --local)")
return cmd
}