-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
70 lines (61 loc) · 1.59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package config
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
var FARGO_VERSION string
// Initialize configuration using Viper
func Load() string { // Load config and return config file path
configDir, err := ConfigDir()
if err != nil {
log.Fatalf("Error getting config file: %v", err)
}
viper.SetEnvPrefix("FARGO")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetConfigName("config")
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
defaultDownload := "~/Downloads"
defaults := map[string]interface{}{
"hub.host": "hoyt.farcaster.xyz",
"hub.port": "2283",
"hub.ssl": "true",
"download.dir": defaultDownload,
"get.count": 20,
"cast.fid": 0,
"cast.privkey": "",
"cast.pubkey": "",
"db.ttlhours": 24,
"pprint.width": 80,
}
for key, value := range defaults {
viper.SetDefault(key, value)
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Printf("Creating %s", filepath.Join(configDir, "config.yaml"))
viper.SafeWriteConfig()
} else {
log.Fatalf("Error reading config file: %v", err)
}
}
downloadDir := viper.GetString("download.dir")
if strings.HasPrefix(downloadDir, "~") {
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Error getting user home directory: %v", err)
}
viper.Set("download.dir", filepath.Join(home, downloadDir[1:]))
}
return viper.ConfigFileUsed()
}
var (
GetString = viper.GetString
GetInt = viper.GetInt
GetBool = viper.GetBool
BindPFlag = viper.BindPFlag
)