/
formatters.go
169 lines (151 loc) · 3.84 KB
/
formatters.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package formatter
import (
"bytes"
"encoding/json"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/krok-o/krokctl/cmd"
)
type kv struct {
Key string
Value interface{}
}
// Formatter defines a formatter for the returned data.
// JSON or Table are supported for now.
type Formatter interface {
FormatObject(data []kv) string
FormatList(data [][]kv) string
}
// JSONFormatter takes a raw data and formats it as JSON data
type JSONFormatter struct{}
// FormatObject formats a key value list as JSON.
func (j *JSONFormatter) FormatObject(data []kv) string {
m := make(map[string]interface{}, len(data))
for _, kv := range data {
m[kv.Key] = kv.Value
}
encoded, err := json.MarshalIndent(m, "", " ")
if err != nil {
cmd.CLILog.Error().Err(err).Msg("Failed to marshal map.")
return ""
}
return string(encoded)
}
// FormatList formats a list of key value objects as JSON.
func (j *JSONFormatter) FormatList(data [][]kv) string {
l := make([]map[string]interface{}, len(data))
for i, kvs := range data {
m := make(map[string]interface{})
for _, kv := range kvs {
m[kv.Key] = kv.Value
}
l[i] = m
}
encoded, err := json.MarshalIndent(l, "", " ")
if err != nil {
cmd.CLILog.Error().Err(err).Msg("Failed to marshal map.")
return ""
}
return string(encoded)
}
// TableFormatter formats data as table entries.
type TableFormatter struct{}
// FormatObject formats a key value list as Table.
func (t *TableFormatter) FormatObject(data []kv) string {
var (
row, header []string
)
for _, v := range data {
header = append(header, v.Key)
row = append(row, convertToString(v.Value))
}
d := [][]string{
row,
}
buf := &bytes.Buffer{}
table := tablewriter.NewWriter(buf)
table.SetHeader(header)
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
for _, v := range d {
table.Append(v)
}
table.Render()
return buf.String()
}
// FormatList formats a list of key value objects as Table.
func (t *TableFormatter) FormatList(data [][]kv) string {
if len(data) == 0 {
return ""
}
// Gather the headers
var header []string
for _, v := range data[0] {
header = append(header, v.Key)
}
// Gather the rows
var d [][]string
for _, kvs := range data {
var row []string
for _, v := range kvs {
row = append(row, convertToString(v.Value))
}
d = append(d, row)
}
buf := &bytes.Buffer{}
table := tablewriter.NewWriter(buf)
table.SetHeader(header)
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
for _, v := range d {
table.Append(v)
}
table.Render()
return buf.String()
}
func dashIfEmpty(v string) string {
if v == "" {
return "-"
}
return v
}
// convertToString converts a value to string. Mostly this will be either an int or a string.
// In the future, I'm going to allow slices and format them as sub tables.
func convertToString(value interface{}) string {
switch v := value.(type) {
case string:
return dashIfEmpty(v)
case int:
return strconv.Itoa(v)
default:
cmd.CLILog.Fatal().Interface("value", v).Msg("Unknown formatting type.")
}
return ""
}
// NewFormatter creates a formatter based on a set argument.
func NewFormatter(opt string) Formatter {
if opt == "json" {
return &JSONFormatter{}
}
return &TableFormatter{}
}