-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
227 lines (189 loc) · 6.51 KB
/
template.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/yarpc/yab/templateargs"
"gopkg.in/yaml.v2"
)
type template struct {
Peers []string `yaml:"peers"`
Peer string `yaml:"peer"`
PeerList string `yaml:"peerList"`
Peerlist stringAlias `yaml:"peerlist"`
PeerDashList stringAlias `yaml:"peer-list"`
Caller string `yaml:"caller"`
Service string `yaml:"service"`
Thrift string `yaml:"thrift"`
Procedure string `yaml:"procedure"`
Method stringAlias `yaml:"method"`
DisableThriftEnvelope *bool `yaml:"disableThriftEnvelope"`
DisableDashThriftEnvelope **bool `yaml:"disable-thrift-envelope"`
Disablethriftenvelope **bool `yaml:"disablethriftenvelope"`
ShardKey string `yaml:"shardKey"`
RoutingKey string `yaml:"routingKey"`
RoutingDelegate string `yaml:"routingDelegate"`
Shardkey stringAlias `yaml:"shardkey"`
Routingkey stringAlias `yaml:"routingkey"`
Routingdelegate stringAlias `yaml:"routingdelegate"`
ShardDashKey stringAlias `yaml:"shard-key"`
RoutingDashKey stringAlias `yaml:"routing-key"`
RoutingDashDelegate stringAlias `yaml:"routing-delegate"`
SK stringAlias `yaml:"sk"`
RK stringAlias `yaml:"rk"`
RD stringAlias `yaml:"rd"`
Headers map[string]string `yaml:"headers"`
Baggage map[string]string `yaml:"baggage"`
Jaeger bool `yaml:"jaeger"`
Request map[interface{}]interface{} `yaml:"request"`
Timeout time.Duration `yaml:"timeout"`
}
func readYAMLFile(yamlTemplate string, templateArgs map[string]string, opts *Options) error {
contents, err := ioutil.ReadFile(yamlTemplate)
if err != nil {
return err
}
base := filepath.Dir(yamlTemplate)
// Ensuring that the base directory is fully qualified. Otherwise, whether it
// is fully qualified depends on argv[0].
// Must be fully qualified to be expressible as a file:/// URL.
// Go’s URL parser does not recognize file:path as host-relative, not-CWD relative.
base, err = filepath.Abs(base)
if err != nil {
return err
}
// Adding a final slash so that the base URL refers to a directory, unless the base is exactly "/".
if !strings.HasSuffix(base, "/") {
base += "/"
}
return readYAMLRequest(base, contents, templateArgs, opts)
}
func readYAMLRequest(base string, contents []byte, templateArgs map[string]string, opts *Options) error {
t, err := unmarshalTemplate(contents)
if err != nil {
return err
}
t.Request, err = templateargs.ProcessMap(t.Request, templateArgs)
if err != nil {
return err
}
body, err := yaml.Marshal(t.Request)
if err != nil {
return err
}
if t.Peer != "" {
opts.TOpts.Peers = []string{t.Peer}
opts.TOpts.PeerList = ""
} else if len(t.Peers) > 0 {
opts.TOpts.Peers = t.Peers
opts.TOpts.PeerList = ""
} else if t.PeerList != "" {
peerListURL, err := resolve(base, t.PeerList)
if err != nil {
return err
}
opts.TOpts.PeerList = peerListURL.String()
opts.TOpts.Peers = nil
}
// Baggage and headers specified with command line flags override those
// specified in YAML templates.
opts.ROpts.Headers = merge(opts.ROpts.Headers, t.Headers)
opts.ROpts.Baggage = merge(opts.ROpts.Baggage, t.Baggage)
if t.Jaeger {
opts.TOpts.Jaeger = true
}
if t.Thrift != "" {
thriftFileURL, err := resolve(base, t.Thrift)
if err != nil {
return err
}
opts.ROpts.ThriftFile = thriftFileURL.Path
}
overrideParam(&opts.TOpts.CallerName, t.Caller)
overrideParam(&opts.TOpts.ServiceName, t.Service)
overrideParam(&opts.ROpts.Procedure, t.Procedure)
overrideParam(&opts.TOpts.ShardKey, t.ShardKey)
overrideParam(&opts.TOpts.RoutingKey, t.RoutingKey)
overrideParam(&opts.TOpts.RoutingDelegate, t.RoutingDelegate)
overrideParam(&opts.ROpts.RequestJSON, string(body))
if t.DisableThriftEnvelope != nil {
opts.ROpts.ThriftDisableEnvelopes = *t.DisableThriftEnvelope
}
if t.Timeout != 0 {
opts.ROpts.Timeout = timeMillisFlag(t.Timeout)
}
return nil
}
func overrideParam(s *string, newS string) {
if newS != "" {
*s = newS
}
}
func unmarshalTemplate(bytes []byte) (*template, error) {
t := &template{}
t.Method.dest = &t.Procedure
t.Peerlist.dest = &t.PeerList
t.PeerDashList.dest = &t.PeerList
t.Shardkey.dest = &t.ShardKey
t.Routingkey.dest = &t.RoutingKey
t.Routingdelegate.dest = &t.RoutingDelegate
t.ShardDashKey.dest = &t.ShardKey
t.RoutingDashKey.dest = &t.RoutingKey
t.RoutingDashDelegate.dest = &t.RoutingDelegate
t.SK.dest = &t.ShardKey
t.RK.dest = &t.RoutingKey
t.RD.dest = &t.RoutingDelegate
t.Disablethriftenvelope = &t.DisableThriftEnvelope
t.DisableDashThriftEnvelope = &t.DisableThriftEnvelope
err := yaml.Unmarshal(bytes, &t)
return t, err
}
type headers map[string]string
// In these cases, the existing item (target, from flags) overrides the source
// (template).
func merge(target, source headers) headers {
if len(source) == 0 {
return target
}
if len(target) == 0 {
return source
}
for k, v := range source {
if _, exists := target[k]; !exists {
target[k] = v
}
}
return target
}
func resolve(base, rel string) (*url.URL, error) {
baseURL := &url.URL{
Scheme: "file",
Path: base,
}
relU, err := url.Parse(rel)
if err != nil {
return nil, err
}
return baseURL.ResolveReference(relU), nil
}