This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
print.go
179 lines (158 loc) · 4.42 KB
/
print.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
174
175
176
177
178
179
package route
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/gogo/protobuf/types"
"github.com/solo-io/gloo/pkg/api/types/v1"
)
// Destination represents a single destination of a route
// It can be either an upstream or upstream-function pair
type Destination struct {
Upstream string
Function string
}
func (d *Destination) String() string {
if d.Function != "" {
return fmt.Sprintf("%s:%s", d.Upstream, d.Function)
}
return d.Upstream
}
// PrintTable prints the list of routes as a table
func PrintTable(list []*v1.Route, w io.Writer) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Id", "Matcher", "Type", "Verb", "Header", "Upstream", "Function", "Extension"})
for i, r := range list {
matcher, rType, verb, headers := Matcher(r)
ext := Extension(r)
for _, d := range Destinations(r) {
table.Append([]string{strconv.Itoa(i + 1), matcher, rType, verb, headers, d.Upstream, d.Function, ext})
}
}
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Render()
}
// Matcher extracts the parts of the matcher in the given route
func Matcher(r *v1.Route) (string, string, string, string) {
switch m := r.GetMatcher().(type) {
case *v1.Route_EventMatcher:
return m.EventMatcher.EventType, "Event", "", ""
case *v1.Route_RequestMatcher:
var path string
var rType string
switch p := m.RequestMatcher.GetPath().(type) {
case *v1.RequestMatcher_PathExact:
path = p.PathExact
rType = "Exact Path"
case *v1.RequestMatcher_PathPrefix:
path = p.PathPrefix
rType = "Path Prefix"
case *v1.RequestMatcher_PathRegex:
path = p.PathRegex
rType = "Regex Path"
default:
path = ""
rType = "Unknown"
}
verb := "*"
if m.RequestMatcher.Verbs != nil {
verb = strings.Join(m.RequestMatcher.Verbs, " ")
}
headers := ""
if m.RequestMatcher.Headers != nil {
builder := bytes.Buffer{}
for k, v := range m.RequestMatcher.Headers {
builder.WriteString(k)
builder.WriteString(":")
builder.WriteString(v)
builder.WriteString("; ")
}
headers = builder.String()
}
return path, rType, verb, headers
default:
return "", "Unknown", "", ""
}
}
// Destinations extracts the list of destinations in a given route
func Destinations(r *v1.Route) []Destination {
single := r.GetSingleDestination()
if single != nil {
return []Destination{upstreamToDestination(single.GetUpstream(), single.GetFunction())}
}
multi := r.GetMultipleDestinations()
if multi != nil {
d := make([]Destination, len(multi))
for i, m := range multi {
d[i] = upstreamToDestination(m.GetUpstream(), m.GetFunction())
}
return d
}
return []Destination{}
}
func upstreamToDestination(u *v1.UpstreamDestination, f *v1.FunctionDestination) Destination {
if u != nil {
return Destination{u.Name, ""}
}
if f != nil {
return Destination{f.UpstreamName, f.FunctionName}
}
return Destination{"", ""}
}
// Extension extracts extensions in a given route as a pretty printed string
func Extension(r *v1.Route) string {
ext := r.GetExtensions()
if ext == nil || ext.GetFields() == nil {
return ""
}
s := make([]string, len(ext.GetFields()))
i := 0
for k, v := range ext.GetFields() {
s[i] = fmt.Sprintf("%s: %s", k, prettyPrint(v))
i++
}
return fmt.Sprintf("{%s}", strings.Join(s, ",\n"))
}
func prettyPrint(v *types.Value) string {
switch t := v.Kind.(type) {
case *types.Value_NullValue:
return ""
case *types.Value_NumberValue:
return fmt.Sprintf("%v", t.NumberValue)
case *types.Value_StringValue:
return fmt.Sprintf("\"%v\"", t.StringValue)
case *types.Value_BoolValue:
return fmt.Sprintf("%v", t.BoolValue)
case *types.Value_StructValue:
return prettyPrintStruct(t)
case *types.Value_ListValue:
return prettyPrintList(t)
default:
return "<unknown>"
}
}
func prettyPrintList(lv *types.Value_ListValue) string {
if lv == nil || lv.ListValue == nil || lv.ListValue.Values == nil {
return ""
}
s := make([]string, len(lv.ListValue.Values))
for i, v := range lv.ListValue.Values {
s[i] = prettyPrint(v)
}
return fmt.Sprintf("[%s]", strings.Join(s, ", "))
}
func prettyPrintStruct(sv *types.Value_StructValue) string {
if sv == nil || sv.StructValue == nil || sv.StructValue.Fields == nil {
return ""
}
s := make([]string, len(sv.StructValue.GetFields()))
i := 0
for k, v := range sv.StructValue.GetFields() {
s[i] = fmt.Sprintf("%s: %s", k, prettyPrint(v))
i++
}
return fmt.Sprintf("{%s}", strings.Join(s, ", "))
}