forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readclient.go
118 lines (94 loc) · 2.49 KB
/
readclient.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
package main
import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/eskipfile"
etcdclient "github.com/zalando/skipper/etcd"
innkeeperclient "github.com/zalando/skipper/innkeeper"
"io"
"io/ioutil"
"os"
)
type readClient interface {
LoadAndParseAll() ([]*eskip.RouteInfo, error)
}
type stdinReader struct {
reader io.Reader
}
type inlineReader struct {
routes string
}
type idsReader struct {
ids []string
}
func createReadClient(m *medium) (readClient, error) {
// no output, no client
if m == nil {
return nil, nil
}
switch m.typ {
case innkeeper:
return createInnkeeperClient(m)
case etcd:
return etcdclient.New(etcdclient.Options{
Endpoints: urlsToStrings(m.urls),
Prefix: m.path,
Insecure: insecure})
case stdin:
return &stdinReader{reader: os.Stdin}, nil
case file:
return eskipfile.Open(m.path)
case inline:
return &inlineReader{routes: m.eskip}, nil
case inlineIds:
return &idsReader{ids: m.ids}, nil
default:
return nil, invalidInputType
}
}
func createInnkeeperClient(m *medium) (*innkeeperclient.Client, error) {
auth := innkeeperclient.CreateInnkeeperAuthentication(innkeeperclient.AuthOptions{InnkeeperAuthToken: m.oauthToken})
ic, err := innkeeperclient.New(innkeeperclient.Options{
Address: m.urls[0].String(),
Insecure: insecure,
Authentication: auth})
if err != nil {
return nil, err
}
return ic, nil
}
func (r *stdinReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
// this pretty much disables continuous piping,
// but since the reset command first upserts all
// and deletes the diff only after, it may not
// even be consistent to do continous piping.
// May change in the future.
doc, err := ioutil.ReadAll(r.reader)
if err != nil {
return nil, err
}
routes, err := eskip.Parse(string(doc))
if err != nil {
return nil, err
}
return routesToRouteInfos(routes), nil
}
func (r *inlineReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
routes, err := eskip.Parse(r.routes)
if err != nil {
return nil, err
}
return routesToRouteInfos(routes), nil
}
func (r *idsReader) LoadAndParseAll() ([]*eskip.RouteInfo, error) {
routeInfos := make([]*eskip.RouteInfo, len(r.ids))
for i, id := range r.ids {
routeInfos[i] = &eskip.RouteInfo{Route: eskip.Route{Id: id}}
}
return routeInfos, nil
}
func routesToRouteInfos(routes []*eskip.Route) (routeInfos []*eskip.RouteInfo) {
for _, route := range routes {
routeInfos = append(routeInfos, &eskip.RouteInfo{Route: *route})
}
return
}