mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-05 21:53:04 -06:00
Merge pull request #8164 from hashicorp/jbardin/GH-8154
Fix panic when there's no state to print
This commit is contained in:
commit
81ea297f1f
@ -45,22 +45,27 @@ func (c *StateShowCommand) Run(args []string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
instance, err := c.filterInstance(results)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
is := instance.Value.(*terraform.InstanceState)
|
||||
|
||||
// Sort the keys
|
||||
keys := make([]string, 0, len(is.Attributes))
|
||||
var keys []string
|
||||
for k, _ := range is.Attributes {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
// Build the output
|
||||
output := make([]string, 0, len(is.Attributes)+1)
|
||||
var output []string
|
||||
output = append(output, fmt.Sprintf("id | %s", is.ID))
|
||||
for _, k := range keys {
|
||||
if k != "id" {
|
||||
|
@ -126,6 +126,29 @@ func TestStateShow_noState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateShow_emptyState(t *testing.T) {
|
||||
state := &terraform.State{}
|
||||
|
||||
statePath := testStateFile(t, state)
|
||||
|
||||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateShowCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(p),
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-state", statePath,
|
||||
"test_instance.foo",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
}
|
||||
|
||||
const testStateShowOutput = `
|
||||
id = bar
|
||||
bar = value
|
||||
|
Loading…
Reference in New Issue
Block a user