-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
177 lines (150 loc) · 4.22 KB
/
profile.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
171
172
173
174
175
176
177
package profile
import (
"io/ioutil"
"os"
"strings"
log "github.com/sirupsen/logrus"
syaml "github.com/smallfish/simpleyaml"
)
// default profile dev
var profile = "default"
var configFolderPath = "./"
var data []byte
var yData *syaml.Yaml
var logLevel = log.InfoLevel
// Logger instances
var Logger = log.New()
func init() {
loadProfile()
}
// ClearData to reinitilize profile
func ClearData() {
yData = nil
}
// SetLogLevel - allowed values "debug", "info", "warn", "error"
func SetLogLevel(level string) {
if level == "debug" {
logLevel = log.DebugLevel
}
if level == "info" {
logLevel = log.InfoLevel
}
if level == "warn" {
logLevel = log.WarnLevel
}
if level == "error" {
logLevel = log.ErrorLevel
}
}
func loadProfile() {
Logger.SetLevel(logLevel)
if envConfigFolderPath := os.Getenv("CONFIG_FOLDER_PATH"); len(envConfigFolderPath) > 0 {
configFolderPath = envConfigFolderPath
Logger.WithField("configPath", configFolderPath).Info("ProfileService: ConfigFolderPath set by enironment variable to")
}
if envProfile := os.Getenv("PROFILE"); len(envProfile) > 0 {
profile = envProfile
Logger.WithField("profile", profile).Info("ProfileService: started with profile")
} else {
Logger.Info("ProfileService: env variable PROFILE not set - application started with default profile")
}
var profileFileName string
if len(profile) > 0 && profile != "default" {
profileFileName = "application-" + profile + ".yml"
} else {
profileFileName = "application.yml"
Logger.Info("ProfileService: Fallback to default profile file")
}
var err error
data, err = ioutil.ReadFile(configFolderPath + profileFileName)
if err != nil {
Logger.WithError(err).WithFields(log.Fields{"profile": profile}).Error("ProfileService: application file for profile not found")
}
yData, err = syaml.NewYaml(data)
if err != nil {
Logger.WithError(err).Error("ProfileService: could not parse profile yaml file")
}
}
// GetValueWithDefault - search for string at given path
// and return given default value if no or emtpy value found
func GetValueWithDefault(path string, defaultValue interface{}) interface{} {
var result interface{}
var err error
switch v := defaultValue.(type) {
case int:
result, err = getValue(path).Int()
case float64:
result, err = getValue(path).Float()
case string:
result, err = getValue(path).String()
default:
result = v
}
if err != nil {
Logger.WithError(err).Debug("could not find value for path " + path)
return defaultValue
}
return result
}
// GetIntValue - search for integer value at given path
// and return 0 if no value found
func GetIntValue(path string) int {
result, err := getValue(path).Int()
if err != nil {
Logger.WithError(err).Debug("problem to extract value for path: " + path)
return 0
}
return result
}
// GetStringValue - search for string value at given path
// and return empty string if no value found
func GetStringValue(path string) string {
result, err := getValue(path).String()
if err != nil {
Logger.WithError(err).Debug("problem to extract value for path: " + path)
return ""
}
return result
}
// GetArrayValues search for array at given path
// and return empty array if no values found
func GetArrayValues(path string) []interface{} {
result, err := getValue(path).Array()
if err != nil {
Logger.WithError(err).Debug("problem to extract value for path: " + path)
var emptyArray []interface{}
return emptyArray
}
return result
}
// GetFloatValue - search for float value at given path
// and return 0.0 if no value found
func GetFloatValue(path string) float64 {
result, err := getValue(path).Float()
if err != nil {
Logger.WithError(err).Debug("problem to extract value for path: " + path)
return 0.0
}
return result
}
// GetBooleanValue searc for boolean value at given path
// and return false if no value found
func GetBooleanValue(path string) bool {
result, err := getValue(path).Bool()
if err != nil {
Logger.WithError(err).Debug("problem to extract value for path: " + path)
return false
}
return result
}
func getValue(path string) *syaml.Yaml {
pathPatrs := strings.Split(path, ".")
if yData == nil {
loadProfile()
}
var current = yData
for i := range pathPatrs {
current = current.Get(pathPatrs[i])
}
return current
}