64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package configs
|
|
|
|
import (
|
|
"log-enricher/pkg/log"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func getInt(key string, required bool) int {
|
|
if required {
|
|
checkKey(key)
|
|
}
|
|
|
|
return viper.GetInt(key)
|
|
}
|
|
|
|
func getString(key string, required bool) string {
|
|
if required {
|
|
checkKey(key)
|
|
}
|
|
|
|
return viper.GetString(key)
|
|
}
|
|
|
|
func getBool(key string, required bool) bool {
|
|
if required {
|
|
checkKey(key)
|
|
}
|
|
|
|
return viper.GetBool(key)
|
|
}
|
|
|
|
func getFloatSlice(key string, required bool) []float64 {
|
|
|
|
stringValues := getStringSlice(key, required)
|
|
var floatValues []float64
|
|
for _, val := range stringValues {
|
|
floatVal, err := strconv.ParseFloat(val, 64)
|
|
if err != nil {
|
|
log.Log.Panic("config value is not float type, err : " + err.Error())
|
|
}
|
|
floatValues = append(floatValues, floatVal)
|
|
}
|
|
|
|
return floatValues
|
|
}
|
|
|
|
func checkKey(key string) {
|
|
if !viper.IsSet(key) {
|
|
log.Log.Panic("Missing key: " + key)
|
|
}
|
|
}
|
|
|
|
func getStringSlice(key string, required bool) []string {
|
|
if required {
|
|
checkKey(key)
|
|
}
|
|
|
|
stringValue := viper.GetString(key)
|
|
return strings.Split(stringValue, ",")
|
|
}
|