updates to internal config module
This commit is contained in:
@ -5,55 +5,15 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// getEnvString returns string from environment variable
|
||||
func getEnvString(env, def string) (val string) { //nolint:deadcode
|
||||
val = os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
func getOSEnv(env, def string) string {
|
||||
if val, ok := os.LookupEnv(strings.ToUpper(env)); ok {
|
||||
return val
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getEnvInt returns int from environment variable
|
||||
func getEnvInt(env string, def int) (ret int) {
|
||||
val := os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
ret, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not numeric: %v\n", env)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getEnvBool returns boolean from environment variable
|
||||
func getEnvBool(env string, def bool) bool {
|
||||
var (
|
||||
err error
|
||||
retVal bool
|
||||
val = os.Getenv(env)
|
||||
)
|
||||
|
||||
if len(val) == 0 {
|
||||
return def
|
||||
} else {
|
||||
retVal, err = strconv.ParseBool(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not boolean: %v\n", env)
|
||||
}
|
||||
}
|
||||
|
||||
return retVal
|
||||
return def
|
||||
}
|
||||
|
||||
// Init initializes the application configuration by reading default values from the struct's tags
|
||||
@ -71,28 +31,27 @@ func Init() *Config {
|
||||
switch info.Type.String() {
|
||||
case "string":
|
||||
var dv string
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = info.DefaultValue.(string)
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*string)
|
||||
flag.StringVar(p, info.Name, getEnvString(info.Name, dv), "("+info.Key+")")
|
||||
flag.StringVar(p, info.Name, dv, "("+info.Key+")")
|
||||
|
||||
case "bool":
|
||||
var dv bool
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = info.DefaultValue.(bool)
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*bool)
|
||||
flag.BoolVar(p, info.Name, getEnvBool(info.Name, dv), "("+info.Key+")")
|
||||
flag.BoolVar(p, info.Name, dv, "("+info.Key+")")
|
||||
|
||||
case "int":
|
||||
var dv int
|
||||
|
||||
if info.DefaultValue != nil {
|
||||
dv = int(info.DefaultValue.(int64))
|
||||
}
|
||||
p := reflect.ValueOf(cfg).Elem().FieldByName(info.Name).Addr().Interface().(*int)
|
||||
flag.IntVar(p, info.Name, getEnvInt(info.Name, dv), "("+info.Key+")")
|
||||
flag.IntVar(p, info.Name, dv, "("+info.Key+")")
|
||||
}
|
||||
}
|
||||
flag.Parse()
|
||||
@ -100,12 +59,6 @@ func Init() *Config {
|
||||
// set logging level
|
||||
cfg.setLogLevel()
|
||||
|
||||
// validate some required values are defined.
|
||||
// need to break this out to a required:"true" struct tag
|
||||
if err = cfg.validate(); err != nil {
|
||||
log.Fatalf("[FATAL] %v", err)
|
||||
}
|
||||
|
||||
// timezone & format configuration
|
||||
cfg.TZoneUTC, _ = time.LoadLocation("UTC")
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user