jira-bot/pkg/config/config.go

65 lines
1.1 KiB
Go
Raw Normal View History

2021-06-03 01:59:32 +03:00
package config
2021-06-02 16:07:53 +03:00
import (
"errors"
"log"
"github.com/spf13/viper"
)
// Load config file from given path
2021-06-03 01:59:32 +03:00
func LoadConfig(file string) (*viper.Viper, error) {
2021-06-02 16:07:53 +03:00
v := viper.New()
2021-06-03 01:59:32 +03:00
v.SetConfigName("./" + file)
2021-06-02 16:07:53 +03:00
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) {
2021-06-03 01:59:32 +03:00
cfgFile, err := LoadConfig("settings")
2021-06-02 16:07:53 +03:00
if err != nil {
return nil, err
}
cfg, err := ParseConfig(cfgFile)
if err != nil {
return nil, err
}
return cfg, nil
}
2021-06-03 01:59:32 +03:00
func GetReply() (*Reply, error) {
cfgFile, err := LoadConfig("reply")
if err != nil {
return nil, err
}
reply := Reply{}
_err := cfgFile.Unmarshal(&reply)
if _err != nil {
log.Printf("unable to decode into struct, %v", err)
return nil, err
}
return &reply, nil
}