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