-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (174 loc) · 4.64 KB
/
main.go
File metadata and controls
193 lines (174 loc) · 4.64 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package output
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/logrusorgru/aurora"
)
// Table .
type Table struct {
Header []string `json:"header"`
Data []Data `json:"data"`
}
// Data .
type Data []string
// Options .
type Options struct {
Header bool
CSV bool
JSON bool
Pretty bool
Debug bool
Error string
MultiLine bool
}
// Result .
type Result struct {
ResultData map[string]interface{} `json:"data,omitempty"`
Result string `json:"result,omitempty"` // `success` or warning/error message
Error string `json:"error,omitempty"`
Info string `json:"info,omitempty"`
}
// RenderJSON renders `data` as a JSON string.
func RenderJSON(data interface{}, opts Options) string {
var jsonBytes bytes.Buffer
enc := json.NewEncoder(&jsonBytes)
// Ensures characters like <>& are not converted to unicode literals.
enc.SetEscapeHTML(false)
if opts.Pretty {
enc.SetIndent("", " ")
}
if err := enc.Encode(data); err != nil {
panic(err)
}
return jsonBytes.String()
}
// RenderError renders an error string.
func RenderError(errorMsg string, opts Options) {
if opts.JSON {
jsonData := Result{
Error: trimQuotes(errorMsg),
}
fmt.Fprintf(os.Stderr, "%s", RenderJSON(jsonData, opts))
} else {
fmt.Fprintf(os.Stderr, "Error: %s", trimQuotes(errorMsg))
}
}
// RenderInfo renders an info string.
func RenderInfo(infoMsg string, opts Options) {
if opts.JSON {
jsonData := Result{
Info: trimQuotes(infoMsg),
}
fmt.Fprintf(os.Stderr, "%s", RenderJSON(jsonData, opts))
} else {
fmt.Fprintf(os.Stderr, "Info: %s", trimQuotes(infoMsg))
}
}
// RenderResult renders a result string with optional key/value data.
func RenderResult(result Result, opts Options) string {
var out bytes.Buffer
if opts.JSON {
return RenderJSON(result, opts)
} else {
if trimQuotes(result.Result) == "success" {
out.WriteString(fmt.Sprintf("Result: %s\n", aurora.Green(trimQuotes(result.Result))))
if len(result.ResultData) != 0 {
for k, v := range result.ResultData {
out.WriteString(fmt.Sprintf("%s: %v\n", k, v))
}
}
} else {
fmt.Printf("Result: %s\n", aurora.Yellow(trimQuotes(result.Result)))
if len(result.ResultData) != 0 {
for k, v := range result.ResultData {
out.WriteString(fmt.Sprintf("%s: %v\n", k, v))
}
}
}
}
return out.String()
}
// RenderOutput renders tabular data.
func RenderOutput(data Table, opts Options) string {
var out bytes.Buffer
if opts.Debug {
out.WriteString(fmt.Sprintf("%s\n", aurora.Yellow("Final result:")))
}
if opts.JSON {
// really basic tabledata to json implementation
var rawData []interface{}
for _, dataValues := range data.Data {
jsonData := make(map[string]interface{})
for indexID, dataValue := range dataValues {
dataHeader := strings.ReplaceAll(strings.ToLower(data.Header[indexID]), " ", "-")
jsonData[dataHeader] = dataValue
}
rawData = append(rawData, jsonData)
}
returnedData := map[string]interface{}{
"data": rawData,
}
return RenderJSON(returnedData, opts)
} else {
// otherwise render a table
if opts.Error != "" {
os.Stderr.WriteString(opts.Error)
}
t := table.NewWriter()
opts.Header = !opts.Header
if opts.Header {
var hRow table.Row
for _, k := range data.Header {
hRow = append(hRow, k)
}
t.AppendHeader(hRow)
}
t.SetOutputMirror(&out)
for _, rowData := range data.Data {
var dRow table.Row
for _, k := range rowData {
dRow = append(dRow, k)
}
t.AppendRow(dRow)
}
t.SetStyle(table.StyleDefault)
t.Style().Options = table.OptionsNoBordersAndSeparators
t.Style().Box.PaddingLeft = "" // trim left space
t.Style().Box.PaddingRight = "\t" // pad right with tab
if !opts.MultiLine {
t.SuppressTrailingSpaces() // suppress the trailing spaces if not multiline
}
if opts.MultiLine {
// stops multiline values bleeding into other columns
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "Value", WidthMax: 75}, // Set specific width for "Value" column if multiline
{Name: "Token", WidthMax: 50}, // Set specific width for "Token" column if multiline
})
}
if opts.CSV {
t.RenderCSV()
return out.String()
}
t.Render()
return out.String()
}
}
// RenderString renders a simple string with no extra formatting.
func RenderString(msg string, opts Options) string {
if opts.JSON {
return RenderJSON(trimQuotes(msg), opts)
}
return fmt.Sprintf("%s\n", trimQuotes(msg))
}
func trimQuotes(s string) string {
if len(s) >= 2 {
if s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
}
return s
}