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