This commit is contained in:
Andrey Belvedersky
2021-06-02 16:07:53 +03:00
commit a4838bb973
7 changed files with 519 additions and 0 deletions

52
pkg/config/config.go Normal file
View File

@ -0,0 +1,52 @@
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
}

16
pkg/config/types.go Normal file
View File

@ -0,0 +1,16 @@
package settings
type Config struct {
Debug bool
Jira JiraConfig
}
type JiraConfig struct {
Url string
Auth AuthConfig
}
type AuthConfig struct {
Username string
Password string
}