/
output.go
72 lines (61 loc) · 1.68 KB
/
output.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
package utils
import (
"bytes"
"encoding/json"
"fmt"
"github.com/olekukonko/tablewriter"
"os"
"reflect"
"strings"
)
func PrintTable(slice interface{}) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
CliError("Parsing data: Expected a list format.")
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetRowSeparator(" ")
table.SetHeaderLine(false)
table.SetAutoFormatHeaders(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.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
headers := make([]string, s.Type().Elem().NumField())
for i := 0; i < s.Type().Elem().NumField(); i++ {
headers[i] = s.Type().Elem().Field(i).Name
}
table.SetHeader(headers)
for i := 0; i < s.Len(); i++ {
row := make([]string, s.Type().Elem().NumField())
for j := 0; j < s.Type().Elem().NumField(); j++ {
value := s.Index(i).Field(j)
row[j] = fmt.Sprintf("%v", value)
}
table.Append(row)
}
table.Render()
}
func PrintJson(body []byte) {
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, body, "", " ")
if err != nil {
CliError("Parsing data: Expected a json format")
}
formattedJson := strings.Replace(prettyJSON.String(), "\\n", "\n", -1)
formattedJson = strings.Replace(formattedJson, "\\t", "\t", -1)
fmt.Println(formattedJson)
}
func PrintHeader(header string) {
fmt.Println(Blue(header))
}