jira-bot/pkg/config/config.go
Andrey Belvedersky a4838bb973 🤖
2021-06-02 16:07:53 +03:00

53 lines
869 B
Go

package settings
import (
"errors"
"log"
"github.com/spf13/viper"
)
// Load config file from given path
func LoadConfig() (*viper.Viper, error) {
v := viper.New()
v.SetConfigName("./settings")
v.AddConfigPath(".")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return nil, errors.New("config file not found")
}
return nil, err
}
return v, nil
}
// Parse config file
func ParseConfig(v *viper.Viper) (*Config, error) {
var c Config
err := v.Unmarshal(&c)
if err != nil {
log.Printf("unable to decode into struct, %v", err)
return nil, err
}
return &c, nil
}
// Get config
func GetConfig() (*Config, error) {
cfgFile, err := LoadConfig()
if err != nil {
return nil, err
}
cfg, err := ParseConfig(cfgFile)
if err != nil {
return nil, err
}
return cfg, nil
}