github.com/wtfutil/wtf@v0.43.0/cfg/parsers.go (about) 1 package cfg 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/olebedev/config" 8 ) 9 10 // ParseAsMapOrList takes a configuration key and attempts to parse it first as a map 11 // and then as a list. Map entries are concatenated as "key/value" 12 func ParseAsMapOrList(ymlConfig *config.Config, configKey string) []string { 13 result := []string{} 14 15 mapItems, err := ymlConfig.Map(configKey) 16 if err == nil { 17 for key, value := range mapItems { 18 result = append(result, fmt.Sprintf("%s/%s", value, key)) 19 } 20 return result 21 } 22 23 listItems := ymlConfig.UList(configKey) 24 for _, listItem := range listItems { 25 result = append(result, listItem.(string)) 26 } 27 28 return result 29 } 30 31 // ParseTimeString takes a configuration key and attempts to parse it first as an int 32 // and then as a duration (int + time unit) 33 func ParseTimeString(cfg *config.Config, configKey string, defaultValue string) time.Duration { 34 i, err := cfg.Int(configKey) 35 if err == nil { 36 return time.Duration(i) * time.Second 37 } 38 39 str := cfg.UString(configKey, defaultValue) 40 d, err := time.ParseDuration(str) 41 if err == nil { 42 return d 43 } 44 45 return time.Second 46 }