github.com/seeker-insurance/kit@v0.0.13/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/seeker-insurance/kit/assets"
     9  	"github.com/spf13/viper"
    10  )
    11  
    12  func Load(envPrefix string, configPath string) error {
    13  	viper.SetConfigType("yaml")
    14  
    15  	if len(configPath) > 0 {
    16  		viper.SetConfigFile(configPath)
    17  		if err := viper.ReadInConfig(); err != nil {
    18  			return err
    19  		}
    20  	} else {
    21  		if data, err := assets.Get("data/bin/config.yaml"); err != nil {
    22  			return err
    23  		} else {
    24  			viper.ReadConfig(bytes.NewBuffer(data))
    25  		}
    26  	}
    27  
    28  	viper.SetEnvPrefix(envPrefix)
    29  	viper.AutomaticEnv()
    30  
    31  	for _, envVar := range viper.GetStringSlice("env") {
    32  		if err := viper.BindEnv(envVar); err != nil {
    33  			return err
    34  		}
    35  		if !viper.IsSet(envVar) {
    36  			return errors.New(fmt.Sprintf("Env var is not set: %s", envVar))
    37  		}
    38  	}
    39  
    40  	return nil
    41  }