-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaumbry.go
68 lines (53 loc) · 1.24 KB
/
aumbry.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
package aumbry
import (
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/ghodss/yaml"
)
// Aumbry Package Constants
const (
YamlFile = "YamlFile"
)
// Aumbry is the core loader type.
type Aumbry struct {
cfgType string
model interface{}
rawData []byte
options map[string]string
}
// New creates a Aumbry loader
func New(cfgType string, model interface{}, options map[string]string) *Aumbry {
inst := new(Aumbry)
inst.cfgType = cfgType
inst.model = model
inst.options = options
return inst
}
// Load fetches the desired config and populates the model
func (a *Aumbry) Load() interface{} {
switch a.cfgType {
case YamlFile:
return loadYaml(a)
default:
panic("Invalid config type")
}
}
func loadYaml(a *Aumbry) interface{} {
filename := a.options["CONFIG_FILENAME"]
searchPaths := strings.Split(a.options["CONFIG_SEARCH_PATH"], ";")
a.rawData = loadFile(filename, searchPaths)
yaml.Unmarshal(a.rawData, &a.model)
return a.model
}
func loadFile(name string, searchPaths []string) []byte {
for _, searchPath := range searchPaths {
fullPath := path.Join(searchPath, name)
raw, err := ioutil.ReadFile(fullPath)
if err == nil {
return raw
}
}
panic(fmt.Errorf("Couldn't find %s in any of the search paths", name))
}