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