Fix panic when showing empty state

Fixes a nil dereference when there's no state to print.

Fix some slice declaration to use the recommended style when allocations
don't matter.
This commit is contained in:
James Bardin 2016-08-12 14:19:42 -04:00
parent 97fcd5f4cc
commit 191d7c1007
1 changed files with 7 additions and 2 deletions

View File

@ -45,22 +45,27 @@ func (c *StateShowCommand) Run(args []string) int {
return 1 return 1
} }
if len(results) == 0 {
return 0
}
instance, err := c.filterInstance(results) instance, err := c.filterInstance(results)
if err != nil { if err != nil {
c.Ui.Error(err.Error()) c.Ui.Error(err.Error())
return 1 return 1
} }
is := instance.Value.(*terraform.InstanceState) is := instance.Value.(*terraform.InstanceState)
// Sort the keys // Sort the keys
keys := make([]string, 0, len(is.Attributes)) var keys []string
for k, _ := range is.Attributes { for k, _ := range is.Attributes {
keys = append(keys, k) keys = append(keys, k)
} }
sort.Strings(keys) sort.Strings(keys)
// Build the output // Build the output
output := make([]string, 0, len(is.Attributes)+1) var output []string
output = append(output, fmt.Sprintf("id | %s", is.ID)) output = append(output, fmt.Sprintf("id | %s", is.ID))
for _, k := range keys { for _, k := range keys {
if k != "id" { if k != "id" {