github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_list.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/terraform/terraform" 8 "github.com/mitchellh/cli" 9 ) 10 11 // StateListCommand is a Command implementation that lists the resources 12 // within a state file. 13 type StateListCommand struct { 14 Meta 15 StateMeta 16 } 17 18 func (c *StateListCommand) Run(args []string) int { 19 args, err := c.Meta.process(args, true) 20 if err != nil { 21 return 1 22 } 23 24 cmdFlags := c.Meta.flagSet("state list") 25 cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") 26 lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.") 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 env := c.Workspace() 40 // Get the state 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 47 if err := state.RefreshState(); err != nil { 48 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 49 return 1 50 } 51 52 stateReal := state.State() 53 if stateReal == nil { 54 c.Ui.Error(fmt.Sprintf(errStateNotFound)) 55 return 1 56 } 57 58 filter := &terraform.StateFilter{State: stateReal} 59 results, err := filter.Filter(args...) 60 if err != nil { 61 c.Ui.Error(fmt.Sprintf(errStateFilter, err)) 62 return cli.RunResultHelp 63 } 64 65 for _, result := range results { 66 if i, ok := result.Value.(*terraform.InstanceState); ok { 67 if *lookupId == "" || i.ID == *lookupId { 68 c.Ui.Output(result.Address) 69 } 70 } 71 } 72 73 return 0 74 } 75 76 func (c *StateListCommand) Help() string { 77 helpText := ` 78 Usage: terraform state list [options] [pattern...] 79 80 List resources in the Terraform state. 81 82 This command lists resources in the Terraform state. The pattern argument 83 can be used to filter the resources by resource or module. If no pattern 84 is given, all resources are listed. 85 86 The pattern argument is meant to provide very simple filtering. For 87 advanced filtering, please use tools such as "grep". The output of this 88 command is designed to be friendly for this usage. 89 90 The pattern argument accepts any resource targeting syntax. Please 91 refer to the documentation on resource targeting syntax for more 92 information. 93 94 Options: 95 96 -state=statefile Path to a Terraform state file to use to look 97 up Terraform-managed resources. By default it will 98 use the state "terraform.tfstate" if it exists. 99 100 -id=ID Restricts the output to objects whose id is ID. 101 102 ` 103 return strings.TrimSpace(helpText) 104 } 105 106 func (c *StateListCommand) Synopsis() string { 107 return "List resources in the state" 108 } 109 110 const errStateFilter = `Error filtering state: %[1]s 111 112 Please ensure that all your addresses are formatted properly.` 113 114 const errStateLoadingState = `Error loading the state: %[1]s 115 116 Please ensure that your Terraform state exists and that you've 117 configured it properly. You can use the "-state" flag to point 118 Terraform at another state file.` 119 120 const errStateNotFound = `No state file was found! 121 122 State management commands require a state file. Run this command 123 in a directory where Terraform has been run or use the -state flag 124 to point the command to a specific state location.`