-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
43 lines (39 loc) · 1016 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
42
43
package main
import (
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/sp0x/torrentd/config"
"github.com/spf13/viper"
"os"
"path"
)
var appConfig config.ViperConfig
func initConfig() {
//We load the default config file
homeDir, _ := homedir.Dir()
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
defaultConfigPath := path.Join(homeDir, ".torrentd")
_ = os.MkdirAll(defaultConfigPath, os.ModePerm)
viper.AddConfigPath(defaultConfigPath)
viper.SetConfigType("yaml")
viper.SetConfigName("torrentd")
}
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
config.SetDefaults(&appConfig)
err = viper.SafeWriteConfig()
if err != nil {
log.Warningf("error while writing default config file: %v\n", err)
}
} else {
log.Warningf("error while reading config file: %v\n", err)
os.Exit(1)
}
}
if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}
}