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