/
detailer.go
146 lines (134 loc) · 3.66 KB
/
detailer.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
package cmd
import (
"fmt"
"os"
"sort"
"github.com/metal-stack/metal-go/api/models"
"github.com/olekukonko/tablewriter"
)
type (
// Detailer prints more details than a Printer
Detailer interface {
Detail(data interface{}) error
}
// TableDetailer produces a human readable model representation
TableDetailer struct {
table *tablewriter.Table
}
// MetalSwitchTableDetailer is a table printer for a MetalSwitch
MetalSwitchTableDetailer struct {
TableDetailer
}
// MetalAnyTableDetailer is a Detailer for a any payload
MetalAnyTableDetailer struct {
TableDetailer
}
)
// NewDetailer create a new Detailer which will print more details about metal objects.
func NewDetailer(format string) (Detailer, error) {
var detailer Detailer
switch format {
case "yaml":
detailer = &YAMLPrinter{}
case "json":
detailer = &JSONPrinter{}
case "table", "wide", "markdown", "template":
detailer = newTableDetailer("custom")
default:
return nil, fmt.Errorf("unknown format:%s", format)
}
return detailer, nil
}
func newTableDetailer(format string) TableDetailer {
table := tablewriter.NewWriter(os.Stdout)
switch format {
case "markdown":
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
default:
table.SetHeaderLine(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetRowLine(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true) // no whitespace in front of every line
}
return TableDetailer{
table: table,
}
}
// Detail is identical to Print for json
func (j JSONPrinter) Detail(data interface{}) error {
return j.Print(data)
}
// Detail is identical to Print for json
func (y YAMLPrinter) Detail(data interface{}) error {
return y.Print(data)
}
// Detail a model in a human readable table
func (t TableDetailer) Detail(data interface{}) error {
switch d := data.(type) {
case []*models.V1SwitchResponse:
MetalSwitchTableDetailer{t}.Detail(d)
default:
MetalAnyTableDetailer{t}.Detail(d)
}
return nil
}
// Detail MetalSwitch
func (m MetalSwitchTableDetailer) Detail(data []*models.V1SwitchResponse) {
sort.SliceStable(data, func(i, j int) bool { return *data[i].ID < *data[j].ID })
d := [][]string{}
for _, sw := range data {
filterBySwp := map[string]models.V1BGPFilter{}
for _, n := range sw.Nics {
swp := *(n.Name)
if n.Filter != nil {
filterBySwp[swp] = *(n.Filter)
}
}
sort.SliceStable(sw.Connections, func(i, j int) bool { return *((*sw.Connections[i]).Nic.Name) < *((*sw.Connections[j]).Nic.Name) })
for _, conn := range sw.Connections {
swp := *conn.Nic.Name
partitionID := ""
if sw.Partition != nil {
partitionID = *sw.Partition.ID
}
f := filterBySwp[swp]
row := []string{partitionID, *sw.RackID, *sw.ID, swp, conn.MachineID}
row = append(row, filterColumns(f, 0)...)
max := len(f.Vnis)
if len(f.Cidrs) > max {
max = len(f.Cidrs)
}
d = append(d, row)
for i := 1; i < max; i++ {
row = append([]string{"", "", "", "", ""}, filterColumns(f, i)...)
d = append(d, row)
}
}
}
m.table.SetHeader([]string{"Partition", "Rack", "Switch", "Port", "Machine", "VNI-Filter", "CIDR-Filter"})
m.table.AppendBulk(d)
m.table.Render()
}
func filterColumns(f models.V1BGPFilter, i int) []string {
v := ""
if len(f.Vnis) > i {
v = f.Vnis[i]
}
c := ""
if len(f.Cidrs) > i {
c = f.Cidrs[i]
}
return []string{v, c}
}
// Detail MetalIP
func (m MetalAnyTableDetailer) Detail(data interface{}) {
y := &YAMLPrinter{}
y.Print(data)
}