-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
173 lines (156 loc) · 4.57 KB
/
printer.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
package table
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
"github.com/cycloidio/cycloid-cli/printer"
"github.com/olekukonko/tablewriter"
)
var (
// reference date format
// DD / MM / YYYY, HH:MM:SS
timeFormat = "02/01/2006, 15:04:05"
)
type Table struct{}
// entryFromStruct is a helper to get struct field name
// which represents the column titles
func entryFromStruct(obj reflect.Value, h []string) []string {
// we can't use obj.NumField() because it will
// create a too big slice since it will include
// the unexported field
values := make([]string, 0)
for _, header := range h {
value := obj.FieldByName(header)
switch value.Kind() {
case reflect.String:
values = append(values, value.String())
case reflect.Uint32:
values = append(values, strconv.FormatInt(int64(value.Uint()), 10))
case reflect.Slice:
// Render Slice of strings
typ := value.Type().Elem()
if typ.Kind() == reflect.String {
stringSlice := make([]string, value.Len())
for i := 0; i < value.Len(); i++ {
stringSlice[i] = value.Index(i).String()
}
values = append(values, strings.Join(stringSlice[:], "\n"))
} else {
values = append(values, value.Kind().String())
}
case reflect.Ptr:
elt := value.Elem()
switch elt.Kind() {
case reflect.String:
values = append(values, elt.String())
case reflect.Uint32:
values = append(values, strconv.FormatInt(int64(elt.Uint()), 10))
case reflect.Int64:
t := time.Unix(elt.Int(), 0)
values = append(values, t.Format(timeFormat))
default:
// in the case we don't support the type, we print it
// for further integration
values = append(values, elt.Kind().String())
}
default:
// in the case we don't support the type, we print it
// for further integration
values = append(values, value.Kind().String())
}
}
return values
}
// headersFromStruct is a helper to get struct field name
// which represents the column titles
func headersFromStruct(obj reflect.Value, opts printer.Options) []string {
// we don't set a size to the slice since `NumField`
// return the number of struct fields including the
// unexported fields
headers := make([]string, 0)
for i := 0; i < obj.NumField(); i++ {
f := obj.Type().Field(i)
// remove unexported fields
if len(f.PkgPath) != 0 {
continue
}
// the following lines help to avoid
// adding pointer to a struct into the table
v := obj.FieldByName(f.Name)
if v.Kind() == reflect.Ptr {
elt := v.Elem()
if elt.Kind() == reflect.Struct {
continue
}
}
headers = append(headers, f.Name)
}
return headers
}
func generate(obj interface{}, opts printer.Options) ([]string, [][]string, error) {
var (
headers []string
entries [][]string
err error
)
// obj can be a list of pointer or a pointer to struct
// we need to handle both cases
rObj := reflect.ValueOf(obj)
switch rObj.Kind() {
// the object is a pointer to a struct:
// example: *models.ExternalBackend
case reflect.Ptr:
// we need to get the Value targetted by this pointer
elt := rObj.Elem()
headers = headersFromStruct(elt, opts)
entries = make([][]string, 1)
entry := entryFromStruct(elt, headers)
entries = append(entries, entry)
// the object is a slice of pointers to a struct
// example: []*models.ExternalBackend
case reflect.Slice, reflect.Array:
if rObj.Len() > 0 {
// it's supposed to be an uniform slice
// create headers from the first element is enough
elt := rObj.Index(0).Elem()
headers = headersFromStruct(elt, opts)
}
entries = make([][]string, rObj.Len())
for i := 0; i < rObj.Len(); i++ {
elt := rObj.Index(i).Elem()
entry := entryFromStruct(elt, headers)
entries = append(entries, entry)
}
// default we return an error to help for further object types
default:
err = fmt.Errorf("unable to get headers for object type: %v", rObj.Kind())
}
return headers, entries, err
}
func (t Table) Print(obj interface{}, opts printer.Options, w io.Writer) error {
// TODO: init the array using the opts
// given by the user
table := tablewriter.NewWriter(w)
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)
headers, entries, err := generate(obj, opts)
if err != nil {
return err
}
table.SetHeader(headers)
table.AppendBulk(entries)
table.Render()
return nil
}