Skip to content
Snippets Groups Projects
Select Git revision
  • 7f84f33c1ce0bbbd4e7ed6e80c87d397f371c7ac
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

config.go

Blame
  • 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
    }