github.com/kevholditch/terraform@v0.9.7-0.20170613192930-9706042ddd51/command/state_show.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/cli" 10 "github.com/ryanuber/columnize" 11 ) 12 13 // StateShowCommand is a Command implementation that shows a single resource. 14 type StateShowCommand struct { 15 Meta 16 StateMeta 17 } 18 19 func (c *StateShowCommand) Run(args []string) int { 20 args = c.Meta.process(args, true) 21 22 cmdFlags := c.Meta.flagSet("state show") 23 cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") 24 if err := cmdFlags.Parse(args); err != nil { 25 return cli.RunResultHelp 26 } 27 args = cmdFlags.Args() 28 29 // Load the backend 30 b, err := c.Backend(nil) 31 if err != nil { 32 c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) 33 return 1 34 } 35 36 // Get the state 37 env := c.Workspace() 38 state, err := b.State(env) 39 if err != nil { 40 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 41 return 1 42 } 43 if err := state.RefreshState(); err != nil { 44 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 45 return 1 46 } 47 48 stateReal := state.State() 49 if stateReal == nil { 50 c.Ui.Error(fmt.Sprintf(errStateNotFound)) 51 return 1 52 } 53 54 filter := &terraform.StateFilter{State: stateReal} 55 results, err := filter.Filter(args...) 56 if err != nil { 57 c.Ui.Error(fmt.Sprintf(errStateFilter, err)) 58 return 1 59 } 60 61 if len(results) == 0 { 62 return 0 63 } 64 65 instance, err := c.filterInstance(results) 66 if err != nil { 67 c.Ui.Error(err.Error()) 68 return 1 69 } 70 71 if instance == nil { 72 return 0 73 } 74 75 is := instance.Value.(*terraform.InstanceState) 76 77 // Sort the keys 78 var keys []string 79 for k, _ := range is.Attributes { 80 keys = append(keys, k) 81 } 82 sort.Strings(keys) 83 84 // Build the output 85 var output []string 86 output = append(output, fmt.Sprintf("id | %s", is.ID)) 87 for _, k := range keys { 88 if k != "id" { 89 output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k])) 90 } 91 } 92 93 // Output 94 config := columnize.DefaultConfig() 95 config.Glue = " = " 96 c.Ui.Output(columnize.Format(output, config)) 97 return 0 98 } 99 100 func (c *StateShowCommand) Help() string { 101 helpText := ` 102 Usage: terraform state show [options] ADDRESS 103 104 Shows the attributes of a resource in the Terraform state. 105 106 This command shows the attributes of a single resource in the Terraform 107 state. The address argument must be used to specify a single resource. 108 You can view the list of available resources with "terraform state list". 109 110 Options: 111 112 -state=statefile Path to a Terraform state file to use to look 113 up Terraform-managed resources. By default it will 114 use the state "terraform.tfstate" if it exists. 115 116 ` 117 return strings.TrimSpace(helpText) 118 } 119 120 func (c *StateShowCommand) Synopsis() string { 121 return "Show a resource in the state" 122 }