jira-bot/pkg/config/config.go

61 lines
1.1 KiB
Go
Raw Permalink 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
2021-06-06 03:46:11 +03:00
func GetConfig(filename string) (*Config, error) {
cfgFile, err := LoadConfig(filename)
2021-06-02 16:07:53 +03:00
if err != nil {
return nil, err
}
cfg, err := ParseConfig(cfgFile)
2021-06-06 03:46:11 +03:00
return cfg, err
2021-06-02 16:07:53 +03:00
}
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
}