-
Notifications
You must be signed in to change notification settings - Fork 343
/
load.go
196 lines (163 loc) · 4.09 KB
/
load.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015 Zalando SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/zalando/skipper/eskip"
)
type loadResult struct {
routes []*eskip.Route
parseErrors map[string]error
}
var invalidRouteExpression = errors.New("one or more invalid route expressions")
// store all loaded routes, even if invalid, and store the
// parse errors if any.
func mapRouteInfo(allInfo []*eskip.RouteInfo) loadResult {
lr := loadResult{make([]*eskip.Route, len(allInfo)), make(map[string]error)}
for i, info := range allInfo {
lr.routes[i] = &info.Route
if info.ParseError != nil {
lr.parseErrors[info.Id] = info.ParseError
}
}
return lr
}
// load routes from input medium.
func loadRoutes(in *medium) (loadResult, error) {
rc, err := createReadClient(in)
if err != nil {
return loadResult{}, err
}
routeInfos, err := rc.LoadAndParseAll()
return mapRouteInfo(routeInfos), err
}
// print parse errors and return a generic error
// if any.
func checkParseErrors(lr loadResult) error {
if len(lr.parseErrors) == 0 {
return nil
}
for id, perr := range lr.parseErrors {
printStderr(id, perr)
}
return invalidRouteExpression
}
// load, parse routes and print parse errors if any.
func loadRoutesChecked(in *medium) ([]*eskip.Route, error) {
lr, err := loadRoutes(in)
if err != nil {
return nil, err
}
return lr.routes, checkParseErrors(lr)
}
func checkRepeatedRouteIds(routes []*eskip.Route) error {
ids := map[string]bool{}
for _, route := range routes {
if ids[route.Id] {
return errors.New("Repeating route with id " + route.Id)
}
ids[route.Id] = true
}
return nil
}
// load and parse routes, ignore parse errors.
func loadRoutesUnchecked(in *medium) []*eskip.Route {
lr, _ := loadRoutes(in)
return lr.routes
}
// command executed for check.
func checkCmd(a cmdArgs) error {
routes, err := loadRoutesChecked(a.in)
if err != nil {
return err
}
return checkRepeatedRouteIds(routes)
}
// command executed for print.
func printCmd(a cmdArgs) error {
lr, err := loadRoutes(a.in)
if err != nil {
return err
}
if printJson {
e := json.NewEncoder(stdout)
e.SetEscapeHTML(false)
if err := e.Encode(lr.routes); err != nil {
return err
}
} else {
for _, r := range lr.routes {
if perr, hasError := lr.parseErrors[r.Id]; hasError {
printStderr(r.Id, perr)
}
}
eskip.Fprint(stdout, eskip.PrettyPrintInfo{Pretty: pretty, IndentStr: indentStr, SortPredicates: sortPredicates}, lr.routes...)
}
if len(lr.parseErrors) > 0 {
return invalidRouteExpression
}
return nil
}
func takePatchFilters(media []*medium) (prep, app []*eskip.Filter, err error) {
for _, m := range media {
var fstr string
switch m.typ {
case patchPrepend, patchAppend:
fstr = m.patchFilters
case patchPrependFile, patchAppendFile:
var b []byte
b, err = os.ReadFile(m.patchFile)
if err != nil {
return
}
fstr = string(b)
default:
continue
}
var fs []*eskip.Filter
fs, err = eskip.ParseFilters(fstr)
if err != nil {
return
}
switch m.typ {
case patchPrepend, patchPrependFile:
prep = append(prep, fs...)
case patchAppend, patchAppendFile:
app = append(app, fs...)
}
}
return
}
func patchCmd(a cmdArgs) error {
pf, af, err := takePatchFilters(a.allMedia)
if err != nil {
return err
}
lr, err := loadRoutesChecked(a.in)
if err != nil {
return err
}
for _, r := range lr {
r.Filters = append(pf, append(r.Filters, af...)...)
if r.Id == "" {
fmt.Fprintln(stdout, r.String())
} else {
fmt.Fprintf(stdout, "%s: %s;\n", r.Id, r.String())
}
}
return nil
}