/
table.go
87 lines (73 loc) · 2.42 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package writer
import (
"fmt"
"io"
"github.com/dineshba/tf-summarize/terraformstate"
"github.com/olekukonko/tablewriter"
)
type TableWriter struct {
mdEnabled bool
changes map[string]terraformstate.ResourceChanges
outputChanges map[string][]string
}
var tableOrder = []string{"import", "add", "update", "recreate", "delete"}
func (t TableWriter) Write(writer io.Writer) error {
tableString := make([][]string, 0, 4)
for _, change := range tableOrder {
changedResources := t.changes[change]
for _, changedResource := range changedResources {
if t.mdEnabled {
tableString = append(tableString, []string{change, fmt.Sprintf("`%s`", changedResource.Address)})
} else {
tableString = append(tableString, []string{change, changedResource.Address})
}
}
}
table := tablewriter.NewWriter(writer)
table.SetHeader([]string{"Change", "Resource"})
table.SetAutoMergeCells(true)
table.AppendBulk(tableString)
if t.mdEnabled {
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
} else {
table.SetRowLine(true)
}
table.Render()
// Disable the Output Summary if there are no outputs to display
if hasOutputChanges(t.outputChanges) {
tableString = make([][]string, 0, 4)
for _, change := range tableOrder {
changedOutputs := t.outputChanges[change]
for _, changedOutput := range changedOutputs {
if t.mdEnabled {
tableString = append(tableString, []string{change, fmt.Sprintf("`%s`", changedOutput)})
} else {
tableString = append(tableString, []string{change, changedOutput})
}
}
}
table = tablewriter.NewWriter(writer)
table.SetHeader([]string{"Change", "Output"})
table.SetAutoMergeCells(true)
table.AppendBulk(tableString)
if t.mdEnabled {
// Without a line break separating each table, a single malformed markdown table is printed.
// Printing an empty newline ensures distinct, separate tables are rendered.
fmt.Fprint(writer, tablewriter.NEWLINE)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
} else {
table.SetRowLine(true)
}
table.Render()
}
return nil
}
func NewTableWriter(changes map[string]terraformstate.ResourceChanges, outputChanges map[string][]string, mdEnabled bool) Writer {
return TableWriter{
changes: changes,
mdEnabled: mdEnabled,
outputChanges: outputChanges,
}
}