From cce881aaddef9ae8b261848ba764d18213980e75 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Fri, 5 Dec 2014 15:38:41 -0800 Subject: [PATCH] command/show: enable printing of remote state --- command/show.go | 66 ++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/command/show.go b/command/show.go index 83384f4e25..a1c7471ec1 100644 --- a/command/show.go +++ b/command/show.go @@ -36,46 +36,50 @@ func (c *ShowCommand) Run(args []string) int { return 1 } + var err, planErr, stateErr error var path string - if len(args) > 0 { - path = args[0] - } else { - // We should use the default state if it exists. - path = DefaultStateFilename - if _, err := os.Stat(DefaultStateFilename); err != nil { - if os.IsNotExist(err) { - c.Ui.Output("No state.") - return 0 - } - } - } - var plan *terraform.Plan var state *terraform.State - - f, err := os.Open(path) - if err != nil { - c.Ui.Error(fmt.Sprintf("Error loading file: %s", err)) - return 1 - } - - var planErr, stateErr error - plan, err = terraform.ReadPlan(f) - if err != nil { - if _, err := f.Seek(0, 0); err != nil { - c.Ui.Error(fmt.Sprintf("Error reading file: %s", err)) + if len(args) > 0 { + path = args[0] + f, err := os.Open(path) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error loading file: %s", err)) return 1 } + defer f.Close() - plan = nil - planErr = err - } - if plan == nil { - state, err = terraform.ReadState(f) + plan, err = terraform.ReadPlan(f) if err != nil { - stateErr = err + if _, err := f.Seek(0, 0); err != nil { + c.Ui.Error(fmt.Sprintf("Error reading file: %s", err)) + return 1 + } + + plan = nil + planErr = err + } + if plan == nil { + state, err = terraform.ReadState(f) + if err != nil { + stateErr = err + } + } + + } else { + // We should use the default state if it exists. + c.Meta.statePath = DefaultStateFilename + state, err = c.Meta.loadState() + if err != nil { + c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) + return 1 + } + if state == nil { + c.Ui.Output("No state.") + return 0 } } + if plan == nil && state == nil { c.Ui.Error(fmt.Sprintf( "Terraform couldn't read the given file as a state or plan file.\n"+