-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
48 lines (44 loc) · 926 Bytes
/
config.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
package env_config_loader
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type Environment struct {
Paths []string
Name string
}
func NewEnv(paths []string, name string) Environment{
return Environment{Paths: paths, Name: name}
}
func (Env Environment) Load(fileName string, v interface{}) error{
pathLength := len(Env.Paths)
var name string = Env.Name
if len(name) == 0 {
name = "local"
}
var error error = nil
for i := 0; i < pathLength; i++ {
path := Env.Paths[i]
configPath := fmt.Sprintf("%s/%s/%s", path, name, fileName)
_, err := os.Stat(configPath)
if err != nil && os.IsNotExist(err) {
continue
}
file, err := ioutil.ReadFile(configPath)
if err != nil {
error = err
break
}
err = yaml.Unmarshal(file, v)
if err != nil {
error = err
break
}
}
if error != nil {
panic(fmt.Sprintf("load config: %s failed, error: %v", fileName, error))
}
return error
}