github.com/inge4pres/terraform@v0.7.5-0.20160930053151-bd083f84f376/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 state, err := c.Meta.State() 30 if err != nil { 31 c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) 32 return cli.RunResultHelp 33 } 34 35 stateReal := state.State() 36 if stateReal == nil { 37 c.Ui.Error(fmt.Sprintf(errStateNotFound)) 38 return 1 39 } 40 41 filter := &terraform.StateFilter{State: stateReal} 42 results, err := filter.Filter(args...) 43 if err != nil { 44 c.Ui.Error(fmt.Sprintf(errStateFilter, err)) 45 return 1 46 } 47 48 if len(results) == 0 { 49 return 0 50 } 51 52 instance, err := c.filterInstance(results) 53 if err != nil { 54 c.Ui.Error(err.Error()) 55 return 1 56 } 57 58 is := instance.Value.(*terraform.InstanceState) 59 60 // Sort the keys 61 var keys []string 62 for k, _ := range is.Attributes { 63 keys = append(keys, k) 64 } 65 sort.Strings(keys) 66 67 // Build the output 68 var output []string 69 output = append(output, fmt.Sprintf("id | %s", is.ID)) 70 for _, k := range keys { 71 if k != "id" { 72 output = append(output, fmt.Sprintf("%s | %s", k, is.Attributes[k])) 73 } 74 } 75 76 // Output 77 config := columnize.DefaultConfig() 78 config.Glue = " = " 79 c.Ui.Output(columnize.Format(output, config)) 80 return 0 81 } 82 83 func (c *StateShowCommand) Help() string { 84 helpText := ` 85 Usage: terraform state show [options] ADDRESS 86 87 Shows the attributes of a resource in the Terraform state. 88 89 This command shows the attributes of a single resource in the Terraform 90 state. The address argument must be used to specify a single resource. 91 You can view the list of available resources with "terraform state list". 92 93 Options: 94 95 -state=statefile Path to a Terraform state file to use to look 96 up Terraform-managed resources. By default it will 97 use the state "terraform.tfstate" if it exists. 98 99 ` 100 return strings.TrimSpace(helpText) 101 } 102 103 func (c *StateShowCommand) Synopsis() string { 104 return "Show a resource in the state" 105 }