package utils import ( "errors" "github.com/mitchellh/mapstructure" "github.com/spf13/viper" ) type ApplicationConfig struct { Token string `mapstructure:"token"` } type ApplicationsConfig map[string]*ApplicationConfig var ErrCannotConvertConfig = errors.New("cannot convert config") func GetApplicationsConfig() (ApplicationsConfig, error) { configRaw := viper.GetStringMap("applications") if configRaw == nil { return nil, ErrCannotConvertConfig } ret := make(ApplicationsConfig) for key, appConfigRaw := range configRaw { if appConfigMap, ok := appConfigRaw.(map[string]interface{}); ok { appConfig := new(ApplicationConfig) err := mapstructure.Decode(appConfigMap, appConfig) if err == nil { ret[key] = appConfig } } } return ret, nil }