github.com/seeker-insurance/kit@v0.0.13/config/required.go (about) 1 package config 2 3 import ( 4 "log" 5 "os" 6 7 "github.com/spf13/viper" 8 ) 9 10 //RequiredString returns the value of the configuration variable specified by key. 11 //If that configuration var does not exist, it calls log.Fatalf 12 func RequiredString(key string) string { 13 FatalCheck(key) 14 return viper.GetString(key) 15 } 16 17 //RequiredInt returns the value of the configuration variable specified by key. 18 //If that configuration var does not exist, it calls log.Fatalf 19 func RequiredInt(key string) int { 20 FatalCheck(key) 21 return viper.GetInt(key) 22 } 23 24 //RequiredFloat64 returns the value of the configuration variable specified by key. 25 //If that configuration var does not exist, it calls log.Fatalf 26 func RequiredFloat64(key string) float64 { 27 FatalCheck(key) 28 return viper.GetFloat64(key) 29 } 30 31 //RequiredStringSlice returns the value of the configuration variable specified by key. 32 //If that configuration var does not exist, it calls log.Fatalf 33 func RequiredStringSlice(key string) []string { 34 FatalCheck(key) 35 return viper.GetStringSlice(key) 36 } 37 38 //FatalCheck checks that a key exists in the viper configuration. If not, it calls fatalf. 39 func FatalCheck(key string) { 40 if viper.Get(key) == nil { 41 log.Fatalf("missing required configuration val: %s", key) 42 } 43 } 44 45 //RequiredEnv checks that the specified environment variable exists. If not, it calls log.Fatalf. 46 func RequiredEnv(key string) string { 47 if val, ok := os.LookupEnv(key); ok { 48 return val 49 } 50 log.Fatalf("missing required environment variable %s", key) 51 return "" 52 }