-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultshandler.go
367 lines (339 loc) · 9.44 KB
/
resultshandler.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package function
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strings"
"unicode"
"unicode/utf8"
)
type ResultsHandler interface {
HandleResults(ctx context.Context, results []any, resultErr error) error
}
type ResultsHandlerFunc func(ctx context.Context, results []any, resultErr error) error
func (f ResultsHandlerFunc) HandleResults(ctx context.Context, results []any, resultErr error) error {
return f(ctx, results, resultErr)
}
// writePaddedTextTable prints a string table to an io.Writer
// padding the table with spaces and using the passed
// columnDelimiter between columns.
func writePaddedTextTable(w io.Writer, rows [][]string, columnDelimiter string) error {
// Collect column widths
var colRuneCount []int
for row := range rows {
for col, str := range rows[row] {
count := utf8.RuneCountInString(str)
if col >= len(colRuneCount) {
colRuneCount = append(colRuneCount, count)
} else if count > colRuneCount[col] {
colRuneCount[col] = count
}
}
}
// Print with padded cell widths and columnDelimiter
line := make([]byte, 0, 1024)
for row := range rows {
// Append cells of row to line
for col, str := range rows[row] {
if col > 0 {
line = append(line, columnDelimiter...)
}
line = append(line, str...)
if pad := colRuneCount[col] - utf8.RuneCountInString(str); pad > 0 {
line = append(line, bytes.Repeat([]byte{' '}, pad)...)
}
}
// In case not all rows have the same number of cells
// pad line with empty cells
for col := len(rows[row]); col < len(colRuneCount); col++ {
if col > 0 {
line = append(line, columnDelimiter...)
}
line = append(line, bytes.Repeat([]byte{' '}, colRuneCount[col])...)
}
line = append(line, '\n')
_, err := w.Write(line)
if err != nil {
return err
}
line = line[:0]
}
return nil
}
// makeResultsPrintable converts the elements of the results slice
// to strings with a readable representation.
// It modifies the results slice in place.
func makeResultsPrintable(results []any) error {
for i, result := range results {
switch x := result.(type) {
case fmt.GoStringer:
results[i] = x.GoString()
case fmt.Stringer:
results[i] = x.String()
case []string:
results[i] = strings.Join(x, "\n")
case []byte:
if utf8.Valid(x) && bytes.IndexFunc(x, func(r rune) bool { return !unicode.IsPrint(r) }) == -1 {
results[i] = string(x)
} else {
results[i] = fmt.Sprintf("%#x", x)
}
case [][]string:
var b strings.Builder
err := writePaddedTextTable(&b, x, "|")
if err != nil {
return fmt.Errorf("can't print command result padded text table because: %w", err)
}
results[i] = b.String()
default:
switch v := derefValue(reflect.ValueOf(result)); v.Kind() {
case reflect.Float32, reflect.Float64:
// Print with up to 12 decimals precission
results[i] = strings.TrimRight(fmt.Sprintf("%.12f", v.Interface()), "0")
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
b, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("can't print command result as JSON because: %w", err)
}
results[i] = string(b)
case reflect.Func, reflect.Chan:
// Use Go source representation for functional types
// that have no useful printable value
results[i] = fmt.Sprintf("%#v", result)
}
}
}
return nil
}
// PrintTo calls fmt.Fprint on writer with the result values as varidic arguments
func PrintTo(writer io.Writer) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
_, err = fmt.Fprint(writer, results...)
return err
}
}
// PrintlnTo calls fmt.Fprintln on writer for every result
func PrintlnTo(writer io.Writer) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
for _, r := range results {
_, err = fmt.Fprintln(writer, r)
if err != nil {
return err
}
}
return nil
}
}
// Println calls fmt.Println for every result
var Println ResultsHandlerFunc = func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
for _, r := range results {
_, err = fmt.Println(r)
if err != nil {
return err
}
}
return nil
}
// PrintlnWithPrefixTo calls fmt.Fprintln(writer, prefix, result) for every result value
func PrintlnWithPrefixTo(prefix string, writer io.Writer) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
for _, result := range results {
_, err = fmt.Fprintln(writer, prefix, result)
if err != nil {
return err
}
}
return nil
}
}
// PrintlnWithPrefix calls fmt.Println(prefix, result) for every result value
func PrintlnWithPrefix(prefix string) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
for _, result := range results {
_, err = fmt.Println(prefix, result)
if err != nil {
return err
}
}
return nil
}
}
// Logger interface
type Logger interface {
Printf(format string, args ...any)
}
// LogTo calls logger.Printf(fmt.Sprintln(results...))
func LogTo(logger Logger) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
logger.Printf(fmt.Sprintln(results...))
return nil
}
}
// LogWithPrefixTo calls logger.Printf(fmt.Sprintln(results...)) with prefix prepended to the results
func LogWithPrefixTo(prefix string, logger Logger) ResultsHandlerFunc {
return func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
err := makeResultsPrintable(results)
if err != nil {
return err
}
results = append([]any{prefix}, results...)
logger.Printf(fmt.Sprintln(results...))
return nil
}
}
// PrintlnText prints a fixed string if a command returns without an error
type PrintlnText string
func (t PrintlnText) HandleResults(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
_, err := fmt.Println(t)
return err
}
// derefValue dereferences a reflect.Value until a non pointer type or nil is found
func derefValue(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Pointer && !v.IsNil() {
v = v.Elem()
}
return v
}
// PrintStructSliceAsTable prints a slice of structs or struct pointers as a padded table
// to os.Stdout using fmt.Sprint to format field values.
var PrintStructSliceAsTable ResultsHandlerFunc = func(ctx context.Context, results []any, resultErr error) error {
if resultErr != nil {
return resultErr
}
if len(results) == 0 {
return nil
}
for i, result := range results {
// Check if all results are slices of the same type
resultType := reflect.TypeOf(result)
if resultType.Kind() != reflect.Slice && resultType.Kind() != reflect.Array {
return fmt.Errorf("expected slice or array, got %T for result %d", result, i)
}
if resultType.Elem().Kind() != reflect.Struct && resultType.Elem().Kind() != reflect.Ptr {
return fmt.Errorf("expected slice/array of structs or struct pointers, got %T for result %d", result, i)
}
isPtr := resultType.Elem().Kind() == reflect.Ptr
structType := resultType.Elem()
if isPtr {
structType = structType.Elem()
}
var header []string
for col := 0; col < structType.NumField(); col++ {
header = append(header, structType.Field(col).Name)
}
rows := [][]string{header}
sliceVal := reflect.ValueOf(result)
for row := 0; row < sliceVal.Len(); row++ {
structVal := sliceVal.Index(row)
if isPtr {
if structVal.IsNil() {
continue
}
structVal = structVal.Elem()
}
fieldStrings := make([]string, structType.NumField())
for col := 0; col < structType.NumField(); col++ {
fieldStrings[col] = fmt.Sprint(structVal.Field(col).Interface())
}
rows = append(rows, fieldStrings)
}
colWidths := make([]int, structType.NumField())
for row := range rows {
for col := 0; col < structType.NumField() && col < len(rows[row]); col++ {
numRunes := utf8.RuneCountInString(rows[row][col])
colWidths[col] = max(colWidths[col], numRunes)
}
}
// Write table to os.Stdout
if i > 0 {
fmt.Println() // Add line between tables
}
w := os.Stdout
var err error
for _, rowStrs := range rows {
for col, colWidth := range colWidths {
switch {
case col == 0:
_, err = w.Write([]byte("| "))
case col < len(colWidths):
_, err = w.Write([]byte(" | "))
}
if err != nil {
return err
}
str := ""
if col < len(rowStrs) {
str = rowStrs[col]
}
_, err = io.WriteString(w, str)
if err != nil {
return err
}
strLen := utf8.RuneCountInString(str)
for i := strLen; i < colWidth; i++ {
_, err = w.Write([]byte{' '})
if err != nil {
return err
}
}
}
_, err = w.Write([]byte(" |\n"))
if err != nil {
return err
}
}
}
return nil
}