-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
84 lines (66 loc) · 1.54 KB
/
config.go
File metadata and controls
84 lines (66 loc) · 1.54 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package config
import (
"os"
"time"
"github.com/joho/godotenv"
"github.com/spf13/viper"
)
type (
Config struct {
NetworkMap map[string]Source `mapstructure:"network_map"`
}
Source struct {
TruthUrl string `mapstructure:"truth_url"`
CheckUrl string `mapstructure:"check_url"`
BaseAsset string `mapstructure:"base_asset"`
// should match most of the time
Deviation float64 `mapstructure:"deviation"`
CronInterval string `mapstructure:"cron_interval"`
}
AccessToken struct {
SlackToken string
SlackChannel string
AppToken string
}
)
func ParseConfig(args []string) (*Config, *AccessToken, error) {
godotenv.Load(".env") //nolint
viper.SetConfigFile(args[0])
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
return nil, nil, err
}
var config Config
err = viper.Unmarshal(&config)
if err != nil {
return nil, nil, err
}
token := viper.GetString("SLACK_TOKEN")
if token == "" {
token = os.Getenv("SLACK_TOKEN")
}
channel := viper.GetString("SLACK_CHANNEL")
if channel == "" {
channel = os.Getenv("SLACK_CHANNEL")
}
appToken := viper.GetString("APP_TOKEN")
if channel == "" {
channel = os.Getenv("APP_TOKEN")
}
accessToken := &AccessToken{
SlackToken: token,
SlackChannel: channel,
AppToken: appToken,
}
return &config, accessToken, config.validate()
}
func (c *Config) validate() error {
// check for cron interval parse
for _, network := range c.NetworkMap {
if _, err := time.ParseDuration(network.CronInterval); err != nil {
return err
}
}
return nil
}