forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
358 lines (321 loc) · 8.02 KB
/
data.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
package main
import (
"fmt"
"io/ioutil"
"log"
"mime"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/blang/vfs"
"github.com/hairyhenderson/gomplate/vault"
)
// logFatal is defined so log.Fatal calls can be overridden for testing
var logFatalf = log.Fatalf
func regExtension(ext, typ string) {
err := mime.AddExtensionType(ext, typ)
if err != nil {
log.Fatal(err)
}
}
func init() {
// Add some types we want to be able to handle which can be missing by default
regExtension(".json", "application/json")
regExtension(".yml", "application/yaml")
regExtension(".yaml", "application/yaml")
regExtension(".csv", "text/csv")
regExtension(".toml", "application/toml")
sourceReaders = make(map[string]func(*Source, ...string) ([]byte, error))
// Register our source-reader functions
addSourceReader("http", readHTTP)
addSourceReader("https", readHTTP)
addSourceReader("file", readFile)
addSourceReader("vault", readVault)
}
var sourceReaders map[string]func(*Source, ...string) ([]byte, error)
// addSourceReader -
func addSourceReader(scheme string, readFunc func(*Source, ...string) ([]byte, error)) {
sourceReaders[scheme] = readFunc
}
// Data -
type Data struct {
Sources map[string]*Source
cache map[string][]byte
}
// NewData - constructor for Data
func NewData(datasourceArgs []string, headerArgs []string) *Data {
sources := make(map[string]*Source)
headers := parseHeaderArgs(headerArgs)
for _, v := range datasourceArgs {
s, err := ParseSource(v)
if err != nil {
log.Fatalf("error parsing datasource %v", err)
return nil
}
s.Header = headers[s.Alias]
sources[s.Alias] = s
}
return &Data{
Sources: sources,
}
}
// Source - a data source
type Source struct {
Alias string
URL *url.URL
Ext string
Type string
Params map[string]string
FS vfs.Filesystem // used for file: URLs, nil otherwise
HC *http.Client // used for http[s]: URLs, nil otherwise
VC *vault.Client //used for vault: URLs, nil otherwise
Header http.Header // used for http[s]: URLs, nil otherwise
}
// NewSource - builds a &Source
func NewSource(alias string, URL *url.URL) (s *Source) {
ext := filepath.Ext(URL.Path)
s = &Source{
Alias: alias,
URL: URL,
Ext: ext,
}
if ext != "" {
mediatype := mime.TypeByExtension(ext)
t, params, err := mime.ParseMediaType(mediatype)
if err != nil {
log.Fatal(err)
}
s.Type = t
s.Params = params
}
return
}
// 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.Type)
}
// ParseSource -
func ParseSource(value string) (*Source, error) {
var (
alias string
srcURL *url.URL
)
parts := strings.SplitN(value, "=", 2)
if len(parts) == 1 {
f := parts[0]
alias = strings.SplitN(value, ".", 2)[0]
if path.Base(f) != f {
err := fmt.Errorf("Invalid datasource (%s). Must provide an alias with files not in working directory", value)
return nil, err
}
srcURL = absURL(f)
} else if len(parts) == 2 {
alias = parts[0]
var err error
srcURL, err = url.Parse(parts[1])
if err != nil {
return nil, err
}
if !srcURL.IsAbs() {
srcURL = absURL(parts[1])
}
}
s := NewSource(alias, srcURL)
return s, nil
}
func absURL(value string) *url.URL {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Can't get working directory: %s", err)
}
baseURL := &url.URL{
Scheme: "file",
Path: cwd + "/",
}
relURL := &url.URL{
Path: value,
}
return baseURL.ResolveReference(relURL)
}
// DatasourceExists -
func (d *Data) DatasourceExists(alias string) bool {
_, ok := d.Sources[alias]
return ok
}
// Datasource -
func (d *Data) Datasource(alias string, args ...string) interface{} {
source, ok := d.Sources[alias]
if !ok {
log.Fatalf("Undefined datasource '%s'", alias)
}
b, err := d.ReadSource(source.FS, source, args...)
if err != nil {
log.Fatalf("Couldn't read datasource '%s': %s", alias, err)
}
s := string(b)
ty := &TypeConv{}
if source.Type == "application/json" {
return ty.JSON(s)
}
if source.Type == "application/yaml" {
return ty.YAML(s)
}
if source.Type == "text/csv" {
return ty.CSV(s)
}
if source.Type == "application/toml" {
return ty.TOML(s)
}
log.Fatalf("Datasources of type %s not yet supported", source.Type)
return nil
}
// Include -
func (d *Data) include(alias string, args ...string) interface{} {
source, ok := d.Sources[alias]
if !ok {
log.Fatalf("Undefined datasource '%s'", alias)
}
b, err := d.ReadSource(source.FS, source, args...)
if err != nil {
log.Fatalf("Couldn't read datasource '%s': %s", alias, err)
}
return string(b)
}
// ReadSource -
func (d *Data) ReadSource(fs vfs.Filesystem, 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
}
if r, ok := sourceReaders[source.URL.Scheme]; ok {
data, err := r(source, args...)
if err != nil {
return nil, err
}
d.cache[cacheKey] = data
return data, nil
}
log.Fatalf("Datasources with scheme %s not yet supported", source.URL.Scheme)
return nil, nil
}
func readFile(source *Source, args ...string) ([]byte, error) {
if source.FS == nil {
source.FS = vfs.OS()
}
// make sure we can access the file
_, err := source.FS.Stat(source.URL.Path)
if err != nil {
log.Fatalf("Can't stat %s: %#v", source.URL.Path, err)
return nil, err
}
f, err := source.FS.OpenFile(source.URL.Path, os.O_RDONLY, 0)
if err != nil {
log.Fatalf("Can't open %s: %#v", source.URL.Path, err)
return nil, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("Can't read %s: %#v", source.URL.Path, err)
return nil, err
}
return b, nil
}
func readHTTP(source *Source, args ...string) ([]byte, error) {
if source.HC == nil {
source.HC = &http.Client{Timeout: time.Second * 5}
}
req, err := http.NewRequest("GET", source.URL.String(), nil)
if err != nil {
return nil, err
}
req.Header = source.Header
res, err := source.HC.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
err := fmt.Errorf("Unexpected HTTP status %d on GET from %s: %s", res.StatusCode, source.URL, string(body))
return nil, err
}
ctypeHdr := res.Header.Get("Content-Type")
if ctypeHdr != "" {
mediatype, params, e := mime.ParseMediaType(ctypeHdr)
if e != nil {
return nil, e
}
source.Type = mediatype
source.Params = params
}
return body, nil
}
func readVault(source *Source, args ...string) ([]byte, error) {
if source.VC == nil {
source.VC = vault.NewClient()
err := source.VC.Login()
addCleanupHook(source.VC.RevokeToken)
if err != nil {
return nil, err
}
}
p := source.URL.Path
if len(args) == 1 {
p = p + "/" + args[0]
}
data, err := source.VC.Read(p)
if err != nil {
return nil, err
}
source.Type = "application/json"
return data, nil
}
func parseHeaderArgs(headerArgs []string) map[string]http.Header {
headers := make(map[string]http.Header)
for _, v := range headerArgs {
ds, name, value := splitHeaderArg(v)
if _, ok := headers[ds]; !ok {
headers[ds] = make(http.Header)
}
headers[ds][name] = append(headers[ds][name], strings.TrimSpace(value))
}
return headers
}
func splitHeaderArg(arg string) (datasourceAlias, name, value string) {
parts := strings.SplitN(arg, "=", 2)
if len(parts) != 2 {
logFatalf("Invalid datasource-header option '%s'", arg)
return "", "", ""
}
datasourceAlias = parts[0]
name, value = splitHeader(parts[1])
return datasourceAlias, name, value
}
func splitHeader(header string) (name, value string) {
parts := strings.SplitN(header, ":", 2)
if len(parts) != 2 {
logFatalf("Invalid HTTP Header format '%s'", header)
return "", ""
}
name = http.CanonicalHeaderKey(parts[0])
value = parts[1]
return name, value
}