-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonfig.go
67 lines (56 loc) · 1.61 KB
/
gonfig.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
package gonfig
import (
"encoding/json"
"fmt"
"os"
"path"
"github.com/eduardbcom/gonfig/internal/config"
"github.com/eduardbcom/gonfig/internal/schema"
)
// Read the configuration json data from
// - config/default.json
// - config/[APP_ENV].json
// - config/local-[APP_END].json
// - CMD --config='{}'
// and merge it into one object.
func Read() ([]byte, error) {
if configDirPath, err := config.GetDirPath(); err != nil {
return nil, err
} else {
var configs []*config.Config
if defaultConfig, err := config.NewFromFile(path.Join(configDirPath, "default.json")); err != nil {
return nil, err
} else {
configs = append(configs, defaultConfig)
}
if appEnv, ok := os.LookupEnv("APP_ENV"); ok {
if envConfig, err := config.NewFromFile(path.Join(configDirPath, fmt.Sprintf("%s.json", appEnv))); err != nil {
return nil, err
} else {
configs = append(configs, envConfig)
}
if localEnvConfig, err := config.NewFromFile(path.Join(configDirPath, fmt.Sprintf("local-%s.json", appEnv))); err != nil {
return nil, err
} else {
configs = append(configs, localEnvConfig)
}
}
if cmdConfig, err := config.NewFromCMD(); err != nil {
return nil, err
} else {
configs = append(configs, cmdConfig)
}
entireConfig := config.Join(configs)
if entireConfig.IsEmpty() {
fmt.Fprintf(os.Stderr, "[GONFIG][WARNING] no configuration data\n")
}
if configSchema, err := schema.New(); err != nil {
return nil, err
} else if configSchema != nil {
if err := configSchema.Validate(entireConfig); err != nil {
return nil, err
}
}
return json.Marshal(entireConfig)
}
}