-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
170 lines (132 loc) · 3.13 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package diaper
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type DiaperConfig struct {
Providers Providers
DefaultEnvFile string
SetMissingEnv bool
}
const (
DefaultEnvFile = ".env"
)
var ErrBuildFilePath = errors.New("failed_to_build_file_path")
func (dc *DiaperConfig) ReadFromFile(env, path string) (ConfigMap, error) {
env = strings.ToLower(env)
if dc.DefaultEnvFile == "" {
dc.DefaultEnvFile = DefaultEnvFile
}
// find the proper .env file to use
// its either ${ENV}.env or dc.DefaultEnvFile
defaultEnvFilePath, err := filepath.Abs(
filepath.Join(path, dc.DefaultEnvFile),
)
if err != nil {
log.Print(fmt.Errorf("file path %s, failed to load: %w", dc.DefaultEnvFile, err))
return nil, err
}
envOrrideFile := fmt.Sprintf("%s.env", env) // ENV=test -> test.env
envFilePath, err := filepath.Abs(
filepath.Join(path, envOrrideFile),
)
if err == nil {
_, err = os.Stat(envFilePath)
}
if err != nil {
// if override file not found use default
// logrus.WithError(err).Warnln("failed to find override env file", envOrrideFile)
// logrus.WithError(err).Debugln("file full path tried ", envFilePath)
envFilePath = defaultEnvFilePath
}
viper.AddConfigPath(path)
viper.SetConfigType("env")
viper.SetConfigFile(envFilePath)
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
log.Fatal(fmt.Errorf("failed to read env file. error %w", err))
}
configMap := ConfigMap{}
if err := viper.Unmarshal(&configMap); err != nil {
log.Fatal(fmt.Errorf("failed to unmarshal config. error %w", err))
}
for key, value := range configMap {
configMap[key] = dc.Providers.Deref(value)
if !dc.SetMissingEnv {
continue
}
// Set the value in ENV if not present
configValue := configMap.MustGetString(key)
if os.Getenv(key) != configValue {
os.Setenv(key, configValue)
}
}
// logrus.Debugln(configMap)
return configMap, nil
}
type ConfigMap map[string]interface{}
func (cmap ConfigMap) Get(key string) (interface{}, bool) {
value, ok := cmap[key]
if !ok {
return nil, false
}
return value, true
}
func (cmap ConfigMap) GetString(key string) (string, bool) {
value, ok := cmap[key]
if !ok {
return "", false
}
strValue, ok := value.(string)
if !ok {
return "", false
}
return strValue, true
}
func (cmap ConfigMap) GetInt(key string) (int, bool) {
value, ok := cmap[key]
if !ok {
return -1, false
}
intVal, ok := value.(int)
if ok {
return intVal, ok
}
strVal, ok := value.(string)
if !ok {
return -1, ok
}
var err error
intVal, err = strconv.Atoi(strVal)
if err != nil {
return -1, false
}
return intVal, true
}
func (cmap ConfigMap) MustGetInt(key string) int {
value, ok := cmap.GetInt(key)
if !ok {
log.Fatal("value for", key, "cannot be cooerced to string")
}
return value
}
func (cmap ConfigMap) MustGet(key string) interface{} {
value, ok := cmap.Get(key)
if !ok {
log.Fatal(key, "not found")
}
return value
}
func (cmap ConfigMap) MustGetString(key string) string {
value, ok := cmap.GetString(key)
if !ok {
return fmt.Sprintf("%v", value)
}
return value
}