-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
41 lines (32 loc) · 779 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package config
import (
"bytes"
"github.com/blessedvictim/frimon-bot/model"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type Config struct {
SlackToken string `mapstructure:"slack_token"`
Jobs []model.Job `mapstructure:"jobs"`
}
var embedRawConfig []byte
func LoadConfig() (*Config, error) {
var err error
viper.SetConfigType("json")
if embedRawConfig != nil {
err = viper.ReadConfig(bytes.NewReader(embedRawConfig))
} else {
viper.SetConfigName("config")
viper.AddConfigPath("./config")
err = viper.ReadInConfig()
}
if err != nil {
return nil, errors.Wrap(err, "cannot read config")
}
var cfg Config
err = viper.Unmarshal(&cfg)
if err != nil {
return nil, errors.Wrap(err, "cannot unmarshal config")
}
return &cfg, nil
}