-
Notifications
You must be signed in to change notification settings - Fork 71
/
config_loaders.go
75 lines (54 loc) · 1.49 KB
/
config_loaders.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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
)
type configLoader interface {
Load(uri string) ([]io.ReadCloser, []string, error)
}
type configLoaderFunc func(string) ([]io.ReadCloser, []string, error)
func (c configLoaderFunc) Load(uri string) ([]io.ReadCloser, []string, error) {
return c(uri)
}
type pollable interface {
Poll()
}
type globFileLoader struct{}
func (globFileLoader) Load(path string) (data []io.ReadCloser, paths []string, err error) {
files, err := filepath.Glob(path)
if err != nil {
return nil, nil, fmt.Errorf("failed to get files from file path (glob) %s, %v", path, err)
}
if len(files) == 0 {
return nil, nil, fmt.Errorf("no files found in path %s", path)
}
var configs []io.ReadCloser
for _, file := range files {
f, err := os.Open(file)
if err != nil {
return nil, nil, fmt.Errorf("could not open %s %v", file, err)
}
configs = append(configs, f)
}
return configs, files, nil
}
func (globFileLoader) Poll() {}
type urlLoader struct{}
func (urlLoader) Load(uri string) ([]io.ReadCloser, []string, error) {
res, err := http.Get(uri)
if err != nil {
return nil, nil, fmt.Errorf("couldn't load config from %s, %v", uri, err)
}
if res.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("did not get 200 from %s, got %d", uri, res.StatusCode)
}
return []io.ReadCloser{res.Body}, []string{uri}, nil
}
func isURL(x string) bool {
u, err := url.Parse(x)
return err == nil && u.Scheme != "" && u.Host != ""
}