2014-07-13 04:47:31 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FormatState takes a state and returns a string
|
|
|
|
func FormatState(s *terraform.State, c *colorstring.Colorize) string {
|
2014-07-13 05:37:30 +02:00
|
|
|
if c == nil {
|
|
|
|
panic("colorize not given")
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
if len(s.Modules) == 0 {
|
2014-07-13 05:37:30 +02:00
|
|
|
return "The state file is empty. No resources are represented."
|
2014-07-13 04:47:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, m := range s.Modules {
|
|
|
|
formatStateModule(&buf, m, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Color(strings.TrimSpace(buf.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatStateModule(buf *bytes.Buffer, m *terraform.ModuleState, c *colorstring.Colorize) {
|
2014-07-13 04:47:31 +02:00
|
|
|
buf.WriteString("[reset]")
|
|
|
|
|
|
|
|
// First get the names of all the resources so we can show them
|
|
|
|
// in alphabetical order.
|
2014-09-17 20:15:07 +02:00
|
|
|
names := make([]string, 0, len(m.Resources))
|
|
|
|
for name, _ := range m.Resources {
|
2014-07-13 04:47:31 +02:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Go through each resource and begin building up the output.
|
|
|
|
for _, k := range names {
|
2014-09-17 20:15:07 +02:00
|
|
|
rs := m.Resources[k]
|
|
|
|
is := rs.Primary
|
|
|
|
id := is.ID
|
2014-07-13 04:47:31 +02:00
|
|
|
if id == "" {
|
|
|
|
id = "<not created>"
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:36:32 +02:00
|
|
|
taintStr := ""
|
2014-09-17 20:15:07 +02:00
|
|
|
if len(rs.Tainted) > 0 {
|
|
|
|
taintStr = " (tainted)"
|
2014-07-22 21:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s:%s\n", k, taintStr))
|
2014-07-13 04:47:31 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(" id = %s\n", id))
|
|
|
|
|
|
|
|
// Sort the attributes
|
2014-09-17 20:15:07 +02:00
|
|
|
attrKeys := make([]string, 0, len(is.Attributes))
|
|
|
|
for ak, _ := range is.Attributes {
|
2014-07-13 04:47:31 +02:00
|
|
|
// Skip the id attribute since we just show the id directly
|
|
|
|
if ak == "id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attrKeys = append(attrKeys, ak)
|
|
|
|
}
|
|
|
|
sort.Strings(attrKeys)
|
|
|
|
|
|
|
|
// Output each attribute
|
|
|
|
for _, ak := range attrKeys {
|
2014-09-17 20:15:07 +02:00
|
|
|
av := is.Attributes[ak]
|
2014-07-13 04:47:31 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s = %s\n", ak, av))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02:00
|
|
|
if len(m.Outputs) > 0 {
|
2014-07-13 04:47:31 +02:00
|
|
|
buf.WriteString("\nOutputs:\n\n")
|
|
|
|
|
|
|
|
// Sort the outputs
|
2014-09-17 20:15:07 +02:00
|
|
|
ks := make([]string, 0, len(m.Outputs))
|
|
|
|
for k, _ := range m.Outputs {
|
2014-07-13 04:47:31 +02:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
// Output each output k/v pair
|
|
|
|
for _, k := range ks {
|
2014-09-17 20:15:07 +02:00
|
|
|
v := m.Outputs[k]
|
2014-07-13 04:47:31 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s = %s\n", k, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|