🤖
This commit is contained in:
52
pkg/config/config.go
Normal file
52
pkg/config/config.go
Normal 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
16
pkg/config/types.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user