forked from jinzhu/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configor.go
69 lines (58 loc) · 1.4 KB
/
configor.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
package configor
import (
"errors"
"os"
"regexp"
)
type Configor struct {
*Config
}
type Config struct {
Environment string
ENVPrefix string
}
// New initialize a Configor
func New(config *Config) *Configor {
if config == nil {
config = &Config{}
}
return &Configor{Config: config}
}
// GetEnvironment get environment
func (configor *Configor) GetEnvironment() string {
if configor.Environment == "" {
if env := os.Getenv("CONFIGOR_ENV"); env != "" {
return env
}
if isTest, _ := regexp.MatchString("/_test/", os.Args[0]); isTest {
return "test"
}
return "development"
}
return configor.Environment
}
// Load will unmarshal configurations to struct from files that you provide
func (configor *Configor) Load(config interface{}, files ...string) error {
cfiles := configor.getConfigurationFiles(files...)
if len(cfiles) == 0 {
return errors.New("Could not find any configuration files")
}
for _, file := range cfiles {
if err := processFile(config, file); err != nil {
return err
}
}
if prefix := configor.getENVPrefix(config); prefix == "-" {
return processTags(config)
} else {
return processTags(config, prefix)
}
}
// ENV return environment
func ENV() string {
return New(nil).GetEnvironment()
}
// Load will unmarshal configurations to struct from files that you provide
func Load(config interface{}, files ...string) error {
return New(nil).Load(config, files...)
}