forked from yunionio/cloudpods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
83 lines (74 loc) · 2.07 KB
/
list.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
package k8s
import (
"fmt"
"reflect"
"strings"
"github.com/gosuri/uitable"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
)
func getPrinterRowValue(printer k8s.ListPrinter, obj jsonutils.JSONObject, col string) interface{} {
getFuncName := fmt.Sprintf("Get%s", strings.Title(col))
manValue := reflect.ValueOf(printer)
funcValue := manValue.MethodByName(getFuncName)
if !funcValue.IsValid() || funcValue.IsNil() {
log.Errorf("Can't get function: %q of manager: %#v", getFuncName, printer)
return nil
}
params := []reflect.Value{
reflect.ValueOf(obj),
}
outs := funcValue.Call(params)
if len(outs) != 1 {
log.Errorf("Invalid return value of function: %q", getFuncName)
return nil
}
return outs[0].Interface()
}
func getPrinterRowValues(printer k8s.ListPrinter, obj jsonutils.JSONObject, cols []string) []interface{} {
ret := make([]interface{}, 0)
for _, col := range cols {
ret = append(ret, getPrinterRowValue(printer, obj, col))
}
return ret
}
func ListerTable(res *modules.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) *uitable.Table {
min := func(x, y int) int {
if x < y {
return x
}
return y
}
table := uitable.New()
table.MaxColWidth = 80
cols := printer.GetColumns(s)
colsI := make([]interface{}, len(cols))
for i, v := range cols {
colsI[i] = v
}
table.AddRow(colsI...)
var idx int
for ; idx < min(res.Limit, res.Total-res.Offset); idx++ {
table.AddRow(getPrinterRowValues(printer, res.Data[idx], cols)...)
}
return table
}
func PrintListResultTable(res *modules.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) {
fmt.Println(ListerTable(res, printer, s))
table := uitable.New()
total := res.Total
offset := res.Offset
limit := res.Limit
page := (offset / limit) + 1
pages := total / limit
if pages*limit < total {
pages += 1
}
table.AddRow("")
table.AddRow("Total", "Pages", "Limit", "Offset", "Page")
table.AddRow(total, pages, limit, offset, page)
fmt.Println(table)
}