github.com/opsidian/terraform@v0.7.8-0.20161104123224-27c39cdfba5b/command/state_rm.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/mitchellh/cli"
     8  )
     9  
    10  // StateRmCommand is a Command implementation that shows a single resource.
    11  type StateRmCommand struct {
    12  	Meta
    13  	StateMeta
    14  }
    15  
    16  func (c *StateRmCommand) Run(args []string) int {
    17  	args = c.Meta.process(args, true)
    18  
    19  	var backupPath string
    20  	cmdFlags := c.Meta.flagSet("state show")
    21  	cmdFlags.StringVar(&backupPath, "backup", "", "backup")
    22  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    23  	if err := cmdFlags.Parse(args); err != nil {
    24  		return cli.RunResultHelp
    25  	}
    26  	args = cmdFlags.Args()
    27  
    28  	state, err := c.StateMeta.State(&c.Meta)
    29  	if err != nil {
    30  		c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
    31  		return cli.RunResultHelp
    32  	}
    33  
    34  	stateReal := state.State()
    35  	if stateReal == nil {
    36  		c.Ui.Error(fmt.Sprintf(errStateNotFound))
    37  		return 1
    38  	}
    39  
    40  	if err := stateReal.Remove(args...); err != nil {
    41  		c.Ui.Error(fmt.Sprintf(errStateRm, err))
    42  		return 1
    43  	}
    44  
    45  	if err := state.WriteState(stateReal); err != nil {
    46  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    47  		return 1
    48  	}
    49  
    50  	if err := state.PersistState(); err != nil {
    51  		c.Ui.Error(fmt.Sprintf(errStateRmPersist, err))
    52  		return 1
    53  	}
    54  
    55  	c.Ui.Output("Item removal successful.")
    56  	return 0
    57  }
    58  
    59  func (c *StateRmCommand) Help() string {
    60  	helpText := `
    61  Usage: terraform state rm [options] ADDRESS...
    62  
    63    Remove one or more items from the Terraform state.
    64  
    65    This command removes one or more items from the Terraform state based
    66    on the address given. You can view and list the available resources
    67    with "terraform state list".
    68  
    69    This command creates a timestamped backup of the state on every invocation.
    70    This can't be disabled. Due to the destructive nature of this command,
    71    the backup is ensured by Terraform for safety reasons.
    72  
    73  Options:
    74  
    75    -backup=PATH        Path where Terraform should write the backup
    76                        state. This can't be disabled. If not set, Terraform
    77                        will write it to the same path as the statefile with
    78                        a backup extension.
    79  
    80    -state=statefile    Path to a Terraform state file to use to look
    81                        up Terraform-managed resources. By default it will
    82                        use the state "terraform.tfstate" if it exists.
    83  
    84  `
    85  	return strings.TrimSpace(helpText)
    86  }
    87  
    88  func (c *StateRmCommand) Synopsis() string {
    89  	return "Remove an item from the state"
    90  }
    91  
    92  const errStateRm = `Error removing items from the state: %s
    93  
    94  The state was not saved. No items were removed from the persisted
    95  state. No backup was created since no modification occurred. Please
    96  resolve the issue above and try again.`
    97  
    98  const errStateRmPersist = `Error saving the state: %s
    99  
   100  The state was not saved. No items were removed from the persisted
   101  state. No backup was created since no modification occurred. Please
   102  resolve the issue above and try again.`