-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
174 lines (153 loc) · 4.62 KB
/
settings.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
package vcago
import (
"flag"
"log"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
)
//Settings represents the global SettingType variable and can be used for load config parameters.
var Settings = SettingHandlerLoad()
//SettingHandler represents and handler for load Settings via flag or environment variable.
type SettingHandler struct {
Error []bool
Config map[string]interface{}
}
//SettingHandlerLoad loads all variables form an .env file and return an SettingHandler.
func SettingHandlerLoad() *SettingHandler {
godotenv.Load(".env")
settings := new(SettingHandler)
settings.Config = make(map[string]interface{})
return settings
}
const (
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
notSet = "is not set in the .env file."
)
//envLogError print all warnings and errors.
func envLogError(key string, e string, lvl string, dVal interface{}) bool {
if lvl == "n" {
return true
}
if lvl == "w" {
log.Print(string(colorYellow), "Warning: ", string(colorWhite), key, " ", e, " Default value: ", dVal)
return true
}
if lvl == "e" {
log.Print(string(colorRed), "Error: ", string(colorWhite), key, " ", e, ". Required for run service.")
return false
}
log.Print(string(colorRed), "Error: ", string(colorWhite), "wrong lvl type. Please set n,w,e.")
return false
}
//settingsLogError print all warnings and errors.
func settingsLogError(key string, e string, lvl string, dVal interface{}) bool {
if lvl == "n" {
return true
}
if lvl == "w" {
log.Print(string(colorYellow), "Warning: ", string(colorWhite), key, " ", e, " Default value: ", dVal)
return true
}
if lvl == "e" {
log.Print(string(colorRed), "Error: ", string(colorWhite), key, " ", e, ". Required for run service.")
return false
}
log.Print(string(colorRed), "Error: ", string(colorWhite), "wrong lvl type. Please set n,w,e.")
return false
}
//stringEnv used to load an string variable from environment variables.
func (i *SettingHandler) stringEnv(key string, lvl string, dVal string) string {
val, ok := os.LookupEnv(key)
if !ok {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
i.Error = append(i.Error, true)
return val
}
//intEnv used to load an int variable from environment variables.
func (i *SettingHandler) intEnv(key string, lvl string, dVal int) int {
val, ok := os.LookupEnv(key)
if !ok {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
valInt, err := strconv.Atoi(val)
if err != nil {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
i.Error = append(i.Error, true)
return valInt
}
//stringListEnv used to load an int variable from environment variables.
func (i *SettingHandler) stringListEnv(key string, lvl string, dVal []string) []string {
val, ok := os.LookupEnv(key)
if !ok {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
valList := strings.Split(val, ",")
if valList == nil {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
i.Error = append(i.Error, true)
return valList
}
//boolEnv load a key from environment variables as bool.
func (i *SettingHandler) boolEnv(key string, lvl string, dVal bool) bool {
val, ok := os.LookupEnv(key)
if !ok {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
if val == "true" {
i.Error = append(i.Error, true)
return true
}
if val == "false" {
i.Error = append(i.Error, true)
return false
}
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
//String loads an string config variable.
//The function will first looking for an flag, than the environment variables and as default dVal.
func (i *SettingHandler) String(key string, lvl string, dVal string) string {
val := flag.String(key, i.stringEnv(key, lvl, dVal), "")
return *val
}
func (i *SettingHandler) Bool(key string, lvl string, dVal bool) bool {
val := flag.Bool(key, i.boolEnv(key, lvl, dVal), "")
return *val
}
func (i *SettingHandler) Int(key string, lvl string, dVal int) int {
val := flag.Int(key, i.intEnv(key, lvl, dVal), "")
return *val
}
func (i *SettingHandler) StringList(key string, lvl string, dVal []string) []string {
ddVal := ""
for n := range dVal {
ddVal = ddVal + dVal[n] + ","
}
val := flag.String(key, i.stringEnv(key, lvl, ddVal), "")
valList := strings.Split(*val, ",")
if valList == nil {
i.Error = append(i.Error, envLogError(key, notSet, lvl, dVal))
return dVal
}
return valList
}
func (i *SettingHandler) Load() {
flag.Parse()
}