You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
781 B
Go
36 lines
781 B
Go
2 years ago
|
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
|
||
|
}
|