-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPConfig.go
52 lines (42 loc) · 1023 Bytes
/
XPConfig.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
package XPSuperKit
import (
"os"
"regexp"
)
type XPConfigImpl struct {
*XPConfigEnvironment
}
type XPConfigEnvironment struct {
Environment string
EnvironmentPrefix string
}
func NewXPConfig(configEnv *XPConfigEnvironment) *XPConfigImpl {
if configEnv == nil {
configEnv = &XPConfigEnvironment{}
}
return &XPConfigImpl{XPConfigEnvironment: configEnv}
}
func (cfg *XPConfigImpl) GetEnvironment() string {
if cfg.Environment == "" {
if env := os.Getenv("XPCONFIG_ENV"); env != "" {
return env
}
if isTest, _ := regexp.MatchString("/_test/", os.Args[0]); isTest {
return "test"
}
return "development"
}
return cfg.Environment
}
func (cfg *XPConfigImpl) Load(config interface{}, files ...string) error {
for _, file := range cfg.getConfigurationFiles(files...) {
if err := processFile(config, file); err != nil {
return err
}
}
if prefix := cfg.getEnvironmentPrefix(config); prefix == "-" {
return processTags(config)
} else {
return processTags(config, prefix)
}
}