Select Git revision
config.go 924 B
package config
import (
"encoding/json"
"os"
"gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logger"
)
func MustGetOrDefault(valuePtr interface{}, name string) {
//update value if defined
envValueStr := os.Getenv(name)
if envValueStr != "" {
if err := json.Unmarshal([]byte(envValueStr), valuePtr); err != nil {
if err := json.Unmarshal([]byte("\""+envValueStr+"\""), valuePtr); err != nil {
panic(errors.Errorf("cannot parse environment %s=%s into %T", name, envValueStr, valuePtr))
}
}
logger.Debugf("CONFIG: Set %s=%s", name, envValueStr)
}
if validator, ok := valuePtr.(Validator); ok {
if err := validator.Validate(); err != nil {
panic(errors.Errorf("Invalid %s=(%T)%+v", name, valuePtr, valuePtr))
}
logger.Debugf("CONFIG: Validated %s", name)
}
logger.Debugf("CONFIG: %s=(%T)%+v", name, valuePtr, valuePtr)
}
type Validator interface {
Validate() error
}