This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
149 lines (131 loc) · 4.04 KB
/
table.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
package utils
/*
* OneLogin AWS Role
* Copyright (c) 2020-2021 Aaron Turner <aturner at synfin dot net>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* I find a lot of reasons to print pretty tables for users. This
* code attempts to be a general purpose solution for that.
*/
import (
"fmt"
"reflect"
"strings"
log "github.com/sirupsen/logrus"
)
const TABLE_HEADER_TAG = "header"
type TableStruct interface {
GetHeader(string) (string, error)
}
// Returns a row and a mapping of struct field name to header names
func TableRow(table TableStruct) (map[string]string, map[string]string) {
row := map[string]string{}
tbl := reflect.ValueOf(table)
fieldCnt := tbl.Type().NumField()
headers := make(map[string]string, fieldCnt)
for i := 0; i < fieldCnt; i++ {
f := reflect.TypeOf(table).Field(i)
header, err := table.GetHeader(f.Name)
if err != nil {
log.Fatal(err)
}
fval := tbl.FieldByName(f.Name)
headers[f.Name] = header
if !fval.IsValid() {
log.Error(fval)
continue // wtf? this shouldn't happen!
}
switch fval.Kind() {
case reflect.String:
row[f.Name] = fval.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
row[f.Name] = fmt.Sprintf("%d", fval.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
row[f.Name] = fmt.Sprintf("%d", fval.Uint())
case reflect.Bool:
if fval.Bool() {
row[f.Name] = "true"
} else {
row[f.Name] = "false"
}
default:
log.Fatalf("Unsupported type: %s", f.Type.Kind())
row[f.Name] = ""
}
}
return row, headers
}
// Geneates a table using a list of TableStruct & struct field names in the report
func GenerateTable(tables []TableStruct, fields []string) {
table := []map[string]string{}
headers := map[string]string{}
for _, item := range tables {
row, h := TableRow(item)
table = append(table, row)
headers = h
}
generateTable(table, headers, fields)
}
func generateTable(data []map[string]string, fieldMap map[string]string, fields []string) {
table := [][]string{}
colWidth := make([]int, len(fields))
// figure out width of column headers
for i, field := range fields {
colWidth[i] = len(fieldMap[field])
}
// calc max len of every column & build our row
for _, r := range data {
row := make([]string, len(fields))
for i, field := range fields {
row[i] = r[field]
if len(r[field]) > colWidth[i] {
colWidth[i] = len(r[field])
}
}
table = append(table, row)
}
// build our fstring for each row
fstrings := make([]string, len(fields))
for i, width := range colWidth {
fstrings[i] = fmt.Sprintf("%%-%ds", width)
}
fstring := strings.Join(fstrings, " | ")
fstring = fmt.Sprintf("%s\n", fstring)
// fmt.Sprintf() expects []interface...
finter := make([]interface{}, len(fields))
for i, field := range fields {
finter[i] = fieldMap[field]
}
// print the header
headerLine := fmt.Sprintf(fstring, finter...)
fmt.Printf("%s%s\n", headerLine, strings.Repeat("=", len(headerLine)-1))
// print each row
for _, row := range data {
values := make([]interface{}, len(fields))
for i, field := range fields {
values[i] = row[field]
}
fmt.Printf(fstring, values...)
}
}
func GetHeaderTag(v reflect.Value, fieldName string) (string, error) {
field, ok := v.Type().FieldByName(fieldName)
if !ok {
return "", fmt.Errorf("Invalid field '%s' in %s", fieldName, v.Type().Name())
}
tag := string(field.Tag.Get(TABLE_HEADER_TAG))
return tag, nil
}