forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
467 lines (416 loc) · 10.9 KB
/
datasource.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package data
import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/spf13/afero"
"github.com/pkg/errors"
"github.com/hairyhenderson/gomplate/libkv"
"github.com/hairyhenderson/gomplate/vault"
)
// stdin - for overriding in tests
var stdin io.Reader
func regExtension(ext, typ string) {
err := mime.AddExtensionType(ext, typ)
if err != nil {
panic(err)
}
}
func init() {
// Add some types we want to be able to handle which can be missing by default
regExtension(".json", jsonMimetype)
regExtension(".yml", yamlMimetype)
regExtension(".yaml", yamlMimetype)
regExtension(".csv", csvMimetype)
regExtension(".toml", tomlMimetype)
regExtension(".env", envMimetype)
}
// registerReaders registers the source-reader functions
func (d *Data) registerReaders() {
d.sourceReaders = make(map[string]func(*Source, ...string) ([]byte, error))
d.sourceReaders["aws+smp"] = readAWSSMP
d.sourceReaders["boltdb"] = readBoltDB
d.sourceReaders["consul"] = readConsul
d.sourceReaders["consul+http"] = readConsul
d.sourceReaders["consul+https"] = readConsul
d.sourceReaders["env"] = readEnv
d.sourceReaders["file"] = readFile
d.sourceReaders["http"] = readHTTP
d.sourceReaders["https"] = readHTTP
d.sourceReaders["merge"] = d.readMerge
d.sourceReaders["stdin"] = readStdin
d.sourceReaders["vault"] = readVault
d.sourceReaders["vault+http"] = readVault
d.sourceReaders["vault+https"] = readVault
}
// lookupReader - return the reader function for the given scheme
func (d *Data) lookupReader(scheme string) (func(*Source, ...string) ([]byte, error), error) {
if d.sourceReaders == nil {
d.registerReaders()
}
r, ok := d.sourceReaders[scheme]
if !ok {
return nil, errors.Errorf("scheme %s not registered", scheme)
}
return r, nil
}
// Data -
type Data struct {
Sources map[string]*Source
sourceReaders map[string]func(*Source, ...string) ([]byte, error)
cache map[string][]byte
// headers from the --datasource-header/-H option that don't reference datasources from the commandline
extraHeaders map[string]http.Header
}
// Cleanup - clean up datasources before shutting the process down - things
// like Logging out happen here
func (d *Data) Cleanup() {
for _, s := range d.Sources {
s.cleanup()
}
}
// NewData - constructor for Data
func NewData(datasourceArgs, headerArgs []string) (*Data, error) {
headers, err := parseHeaderArgs(headerArgs)
if err != nil {
return nil, err
}
data := &Data{
Sources: make(map[string]*Source),
extraHeaders: headers,
}
for _, v := range datasourceArgs {
s, err := parseSource(v)
if err != nil {
return nil, errors.Wrapf(err, "error parsing datasource")
}
s.header = headers[s.Alias]
// pop the header out of the map, so we end up with only the unreferenced ones
delete(headers, s.Alias)
data.Sources[s.Alias] = s
}
return data, nil
}
// Source - a data source
type Source struct {
Alias string
URL *url.URL
mediaType string
fs afero.Fs // used for file: URLs, nil otherwise
hc *http.Client // used for http[s]: URLs, nil otherwise
vc *vault.Vault // used for vault: URLs, nil otherwise
kv *libkv.LibKV // used for consul:, etcd:, zookeeper: & boltdb: URLs, nil otherwise
asmpg awssmpGetter // used for aws+smp:, nil otherwise
header http.Header // used for http[s]: URLs, nil otherwise
}
func (s *Source) inherit(parent *Source) {
s.fs = parent.fs
s.hc = parent.hc
s.vc = parent.vc
s.kv = parent.kv
s.asmpg = parent.asmpg
}
func (s *Source) cleanup() {
if s.vc != nil {
s.vc.Logout()
}
if s.kv != nil {
s.kv.Logout()
}
}
// mimeType returns the MIME type to use as a hint for parsing the datasource.
// It's expected that the datasource will have already been read before
// this function is called, and so the Source's Type property may be already set.
//
// The MIME type is determined by these rules:
// 1. the 'type' URL query parameter is used if present
// 2. otherwise, the Type property on the Source is used, if present
// 3. otherwise, a MIME type is calculated from the file extension, if the extension is registered
// 4. otherwise, the default type of 'text/plain' is used
func (s *Source) mimeType() (mimeType string, err error) {
mediatype := s.URL.Query().Get("type")
if mediatype == "" {
mediatype = s.mediaType
}
if mediatype == "" {
ext := filepath.Ext(s.URL.Path)
mediatype = mime.TypeByExtension(ext)
}
if mediatype != "" {
t, _, err := mime.ParseMediaType(mediatype)
if err != nil {
return "", err
}
mediatype = t
return mediatype, nil
}
return textMimetype, nil
}
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (s *Source) String() string {
return fmt.Sprintf("%s=%s (%s)", s.Alias, s.URL.String(), s.mediaType)
}
// parseSource creates a *Source by parsing the value provided to the
// --datasource/-d commandline flag
func parseSource(value string) (source *Source, err error) {
source = &Source{}
parts := strings.SplitN(value, "=", 2)
if len(parts) == 1 {
f := parts[0]
source.Alias = strings.SplitN(value, ".", 2)[0]
if path.Base(f) != f {
err = errors.Errorf("Invalid datasource (%s). Must provide an alias with files not in working directory", value)
return nil, err
}
source.URL, err = absURL(f)
if err != nil {
return nil, err
}
} else if len(parts) == 2 {
source.Alias = parts[0]
source.URL, err = parseSourceURL(parts[1])
if err != nil {
return nil, err
}
}
return source, nil
}
func parseSourceURL(value string) (*url.URL, error) {
if value == "-" {
value = "stdin://"
}
value = filepath.ToSlash(value)
// handle absolute Windows paths
volName := ""
if volName = filepath.VolumeName(value); volName != "" {
// handle UNCs
if len(volName) > 2 {
value = "file:" + value
} else {
value = "file:///" + value
}
}
srcURL, err := url.Parse(value)
if err != nil {
return nil, err
}
if volName != "" {
if strings.HasPrefix(srcURL.Path, "/") && srcURL.Path[2] == ':' {
srcURL.Path = srcURL.Path[1:]
}
}
if !srcURL.IsAbs() {
srcURL, err = absURL(value)
if err != nil {
return nil, err
}
}
return srcURL, nil
}
func absURL(value string) (*url.URL, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrapf(err, "can't get working directory")
}
urlCwd := filepath.ToSlash(cwd)
baseURL := &url.URL{
Scheme: "file",
Path: urlCwd + "/",
}
relURL := &url.URL{
Path: value,
}
resolved := baseURL.ResolveReference(relURL)
// deal with Windows drive letters
if !strings.HasPrefix(urlCwd, "/") && resolved.Path[2] == ':' {
resolved.Path = resolved.Path[1:]
}
return resolved, nil
}
// DefineDatasource -
func (d *Data) DefineDatasource(alias, value string) (string, error) {
if alias == "" {
return "", errors.New("datasource alias must be provided")
}
if d.DatasourceExists(alias) {
return "", nil
}
srcURL, err := parseSourceURL(value)
if err != nil {
return "", err
}
s := &Source{
Alias: alias,
URL: srcURL,
header: d.extraHeaders[alias],
}
if d.Sources == nil {
d.Sources = make(map[string]*Source)
}
d.Sources[alias] = s
return "", nil
}
// DatasourceExists -
func (d *Data) DatasourceExists(alias string) bool {
_, ok := d.Sources[alias]
return ok
}
func (d *Data) lookupSource(alias string) (*Source, error) {
source, ok := d.Sources[alias]
if !ok {
srcURL, err := url.Parse(alias)
if err != nil || !srcURL.IsAbs() {
return nil, errors.Errorf("Undefined datasource '%s'", alias)
}
source = &Source{
Alias: alias,
URL: srcURL,
header: d.extraHeaders[alias],
}
d.Sources[alias] = source
}
if source.Alias == "" {
source.Alias = alias
}
return source, nil
}
func (d *Data) readDataSource(alias string, args ...string) (data, mimeType string, err error) {
source, err := d.lookupSource(alias)
if err != nil {
return "", "", err
}
b, err := d.readSource(source, args...)
if err != nil {
return "", "", errors.Wrapf(err, "Couldn't read datasource '%s'", alias)
}
mimeType, err = source.mimeType()
if err != nil {
return "", "", err
}
return string(b), mimeType, nil
}
// Include -
func (d *Data) Include(alias string, args ...string) (string, error) {
data, _, err := d.readDataSource(alias, args...)
return data, err
}
// Datasource -
func (d *Data) Datasource(alias string, args ...string) (interface{}, error) {
data, mimeType, err := d.readDataSource(alias, args...)
if err != nil {
return nil, err
}
return parseData(mimeType, data)
}
func parseData(mimeType, s string) (out interface{}, err error) {
switch mimeType {
case jsonMimetype:
out, err = JSON(s)
case jsonArrayMimetype:
out, err = JSONArray(s)
case yamlMimetype:
out, err = YAML(s)
case csvMimetype:
out, err = CSV(s)
case tomlMimetype:
out, err = TOML(s)
case envMimetype:
out, err = dotEnv(s)
case textMimetype:
out = s
default:
return nil, errors.Errorf("Datasources of type %s not yet supported", mimeType)
}
return out, err
}
// DatasourceReachable - Determines if the named datasource is reachable with
// the given arguments. Reads from the datasource, and discards the returned data.
func (d *Data) DatasourceReachable(alias string, args ...string) bool {
source, ok := d.Sources[alias]
if !ok {
return false
}
_, err := d.readSource(source, args...)
return err == nil
}
// readSource returns the (possibly cached) data from the given source,
// as referenced by the given args
func (d *Data) readSource(source *Source, args ...string) ([]byte, error) {
if d.cache == nil {
d.cache = make(map[string][]byte)
}
cacheKey := source.Alias
for _, v := range args {
cacheKey += v
}
cached, ok := d.cache[cacheKey]
if ok {
return cached, nil
}
r, err := d.lookupReader(source.URL.Scheme)
if err != nil {
return nil, errors.Wrap(err, "Datasource not yet supported")
}
data, err := r(source, args...)
if err != nil {
return nil, err
}
d.cache[cacheKey] = data
return data, nil
}
func readStdin(source *Source, args ...string) ([]byte, error) {
if stdin == nil {
stdin = os.Stdin
}
b, err := ioutil.ReadAll(stdin)
if err != nil {
return nil, errors.Wrapf(err, "Can't read %s", stdin)
}
return b, nil
}
func readConsul(source *Source, args ...string) (data []byte, err error) {
if source.kv == nil {
source.kv, err = libkv.NewConsul(source.URL)
if err != nil {
return nil, err
}
err = source.kv.Login()
if err != nil {
return nil, err
}
}
p := source.URL.Path
if len(args) == 1 {
p = p + "/" + args[0]
}
data, err = source.kv.Read(p)
if err != nil {
return nil, err
}
return data, nil
}
func readBoltDB(source *Source, args ...string) (data []byte, err error) {
if source.kv == nil {
source.kv, err = libkv.NewBoltDB(source.URL)
if err != nil {
return nil, err
}
}
if len(args) != 1 {
return nil, errors.New("missing key")
}
p := args[0]
data, err = source.kv.Read(p)
if err != nil {
return nil, err
}
return data, nil
}