command/show: enable printing of remote state

This commit is contained in:
Armon Dadgar 2014-12-05 15:38:41 -08:00 committed by Mitchell Hashimoto
parent 5f35c7852f
commit cce881aadd
1 changed files with 35 additions and 31 deletions

View File

@ -36,46 +36,50 @@ func (c *ShowCommand) Run(args []string) int {
return 1 return 1
} }
var err, planErr, stateErr error
var path string 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 plan *terraform.Plan
var state *terraform.State var state *terraform.State
if len(args) > 0 {
f, err := os.Open(path) path = args[0]
if err != nil { f, err := os.Open(path)
c.Ui.Error(fmt.Sprintf("Error loading file: %s", err)) if err != nil {
return 1 c.Ui.Error(fmt.Sprintf("Error loading file: %s", err))
}
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))
return 1 return 1
} }
defer f.Close()
plan = nil plan, err = terraform.ReadPlan(f)
planErr = err
}
if plan == nil {
state, err = terraform.ReadState(f)
if err != nil { 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 { if plan == nil && state == nil {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"Terraform couldn't read the given file as a state or plan file.\n"+ "Terraform couldn't read the given file as a state or plan file.\n"+