github.com/serbaut/terraform@v0.6.12-0.20160607213102-ac2d195cc560/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 } 16 17 func (c *StateListCommand) Run(args []string) int { 18 args = c.Meta.process(args, true) 19 20 cmdFlags := c.Meta.flagSet("state list") 21 cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") 22 if err := cmdFlags.Parse(args); err != nil { 23 return cli.RunResultHelp 24 } 25 args = cmdFlags.Args() 26 27 state, err := c.State() 28 if err != nil { 29 c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) 30 return cli.RunResultHelp 31 } 32 33 stateReal := state.State() 34 if stateReal == nil { 35 c.Ui.Error(fmt.Sprintf(errStateNotFound)) 36 return 1 37 } 38 39 filter := &terraform.StateFilter{State: stateReal} 40 results, err := filter.Filter(args...) 41 if err != nil { 42 c.Ui.Error(fmt.Sprintf(errStateFilter, err)) 43 return cli.RunResultHelp 44 } 45 46 for _, result := range results { 47 if _, ok := result.Value.(*terraform.InstanceState); ok { 48 c.Ui.Output(result.Address) 49 } 50 } 51 52 return 0 53 } 54 55 func (c *StateListCommand) Help() string { 56 helpText := ` 57 Usage: terraform state list [options] [pattern...] 58 59 List resources in the Terraform state. 60 61 This command lists resources in the Terraform state. The pattern argument 62 can be used to filter the resources by resource or module. If no pattern 63 is given, all resources are listed. 64 65 The pattern argument is meant to provide very simple filtering. For 66 advanced filtering, please use tools such as "grep". The output of this 67 command is designed to be friendly for this usage. 68 69 The pattern argument accepts any resource targeting syntax. Please 70 refer to the documentation on resource targeting syntax for more 71 information. 72 73 Options: 74 75 -state=statefile Path to a Terraform state file to use to look 76 up Terraform-managed resources. By default it will 77 use the state "terraform.tfstate" if it exists. 78 79 ` 80 return strings.TrimSpace(helpText) 81 } 82 83 func (c *StateListCommand) Synopsis() string { 84 return "List resources in the state" 85 } 86 87 const errStateFilter = `Error filtering state: %[1]s 88 89 Please ensure that all your addresses are formatted properly.` 90 91 const errStateLoadingState = `Error loading the state: %[1]s 92 93 Please ensure that your Terraform state exists and that you've 94 configured it properly. You can use the "-state" flag to point 95 Terraform at another state file.` 96 97 const errStateNotFound = `No state file was found! 98 99 State management commands require a state file. Run this command 100 in a directory where Terraform has been run or use the -state flag 101 to point the command to a specific state location.`