command/state_list.go: fix bug loading user-defined state (#21015)

* command/state_list.go: fix bug loading user-defined state

If the user supplied a state path via the `-state` flag and terraform
was running in a workspace other than `default`, the state was not being
loaded properly. Fixes #19920
This commit is contained in:
Kristin Laemmert 2019-04-15 12:22:07 -04:00 committed by GitHub
parent 8645b9703c
commit c1079b59bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 2 deletions

View File

@ -23,14 +23,19 @@ func (c *StateListCommand) Run(args []string) int {
return 1
}
var statePath string
cmdFlags := c.Meta.defaultFlagSet("state list")
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
cmdFlags.StringVar(&statePath, "state", "", "path")
lookupId := cmdFlags.String("id", "", "Restrict output to paths with a resource having the specified ID.")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
}
args = cmdFlags.Args()
if statePath != "" {
c.Meta.statePath = statePath
}
// Load the backend
b, backendDiags := c.Backend(nil)
if backendDiags.HasErrors() {
@ -46,7 +51,7 @@ func (c *StateListCommand) Run(args []string) int {
return 1
}
if err := stateMgr.RefreshState(); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to refresh state: %s", err))
c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err))
return 1
}