-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
53 lines (44 loc) · 1.17 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"flag"
"log"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/train-cat/client-train-go"
"github.com/train-cat/watcher-transilien/cache"
"github.com/train-cat/watcher-transilien/sncf"
"github.com/train-cat/watcher-transilien/utils"
)
func init() {
cfgFile := flag.String("config", "", "config file")
flag.Parse()
if *cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(*cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
// Search config in home directory with name ".rc" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName("config")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Fatal(err)
}
// Init function should be call after viper initialisation
utils.Init()
sncf.Init()
cache.Init()
traincat.SetConfig(traincat.Config{
Host: viper.GetString("api-train.host"),
Auth: traincat.Auth{
Username: viper.GetString("api-train.username"),
Password: viper.GetString("api-train.password"),
},
})
}