pulumi/cmd/stack_rm.go
joeduffy 2eb86b24c2 Make some updates based on CR feedback
This change implements some feedback from @ellismg.

* Make backend.Stack an interface and let backends implement it,
  enabling dynamic type testing/casting to access information
  specific to that backend.  For instance, the cloud.Stack conveys
  the cloud URL, org name, and PPC name, for each stack.

* Similarly expose specialized backend.Backend interfaces,
  local.Backend and cloud.Backend, to convey specific information.

* Redo a bunch of the commands in terms of these.

* Keeping with this theme, turn the CreateStack options into an
  opaque interface{}, and let the specific backends expose their
  own structures with their own settings (like PPC name in cloud).

* Show both the org and PPC names in the cloud column printed in
  the stack ls command, in addition to the Pulumi Cloud URL.

Unrelated, but useful:

* Special case the 401 HTTP response and make a friendly error,
  to tell the developer they must use `pulumi login`.  This is
  better than tossing raw "401: Unauthorized" errors in their face.

* Change the "Updating stack '..' in the Pulumi Cloud" message to
  use the correct action verb ("Previewing", "Destroying", etc).
2017-12-03 08:10:50 -08:00

75 lines
2 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend/state"
"github.com/pulumi/pulumi/pkg/diag/colors"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newStackRmCmd() *cobra.Command {
var yes bool
var force bool
var cmd = &cobra.Command{
Use: "rm [<stack-name>]",
Args: cmdutil.MaximumNArgs(1),
Short: "Remove an stack and its configuration",
Long: "Remove an stack and its configuration\n" +
"\n" +
"This command removes an stack and its configuration state. Please refer to the\n" +
"`destroy` command for removing a resources, as this is a distinct operation.\n" +
"\n" +
"After this command completes, the stack will no longer be available for updates.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Use the stack provided or, if missing, default to the current one.
var stack tokens.QName
if len(args) > 0 {
stack = tokens.QName(args[0])
}
s, err := requireStack(stack)
if err != nil {
return err
}
// Ensure the user really wants to do this.
if yes ||
confirmPrompt("This will permanently remove the '%v' stack!", string(s.Name())) {
hasResources, err := s.Remove(force)
if err != nil {
if hasResources {
return errors.Errorf(
"'%v' still has resources; removal rejected; pass --force to override", s.Name())
}
return err
}
msg := fmt.Sprintf("%sStack '%s' has been removed!%s", colors.SpecAttention, s.Name(), colors.Reset)
fmt.Println(colors.ColorizeText(msg))
if err = state.SetCurrentStack(""); err != nil {
return err
}
}
return nil
}),
}
cmd.PersistentFlags().BoolVarP(
&force, "force", "f", false,
"By default, removal of a stack with resources will be rejected; this forces it")
cmd.PersistentFlags().BoolVar(
&yes, "yes", false,
"Skip confirmation prompts, and proceed with removal anyway")
return cmd
}