This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
118 lines (90 loc) · 2.97 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"github.com/dkulchenko/watchdb/ssl"
"io/ioutil"
"os"
"path"
"strconv"
yaml "gopkg.in/yaml.v2"
)
type WatchConfig struct {
BindAddr string `yaml:"bind_addr,omitempty"`
BindPort string `yaml:"bind_port,omitempty"`
NoBackup bool `yaml:"no_backup,omitempty"`
UseSSL bool `yaml:"use_ssl,omitempty"`
SSLKeyFile string `yaml:"ssl_key_file,omitempty"`
SSLCertFile string `yaml:"ssl_cert_file,omitempty"`
SkipSSLVerify bool `yaml:"skip_ssl_verify,omitempty"`
AuthKey string `yaml:"auth_key,omitempty"`
SyncFile string `yaml:"sync_file,omitempty"`
RemoteConn string `yaml:"remote_conn,omitempty"`
SyncInterval int64 `yaml:"sync_interval,omitempty"`
}
func loadConfig(arguments map[string]interface{}) WatchConfig {
initialConfig := WatchConfig{
BindAddr: "0.0.0.0",
BindPort: "8144",
NoBackup: false,
UseSSL: false,
SkipSSLVerify: false,
SyncInterval: 1000,
}
config_file, ok := arguments["--config-file"].(string)
if ok {
config, err := ioutil.ReadFile(config_file)
if err != nil {
log.Fatalf("error reading config file: %v", err)
}
err = yaml.Unmarshal(config, &initialConfig)
if err != nil {
log.Fatalf("error reading config file: %v", err)
}
log.Notice("config file loaded")
}
if bindaddr, ok := arguments["--bind-addr"].(string); ok {
initialConfig.BindAddr = bindaddr
}
if bindport, ok := arguments["--bind-port"].(string); ok {
initialConfig.BindPort = bindport
}
if nobackup, ok := arguments["--bind-port"].(bool); ok {
initialConfig.NoBackup = nobackup
}
if usessl, ok := arguments["--ssl"].(bool); ok {
initialConfig.UseSSL = usessl
}
if sslkeyfile, ok := arguments["--ssl-key-file"].(string); ok {
initialConfig.SSLKeyFile = sslkeyfile
}
if sslcertfile, ok := arguments["--ssl-cert-file"].(string); ok {
initialConfig.SSLCertFile = sslcertfile
}
if skipsslverify, ok := arguments["--ssl-skip-verify"].(bool); ok {
initialConfig.SkipSSLVerify = skipsslverify
}
if authkey, ok := arguments["--auth-key"].(string); ok {
initialConfig.AuthKey = authkey
}
if syncinterval, ok := arguments["--sync-interval"].(string); ok {
interval, err := strconv.ParseInt(syncinterval, 10, 32)
if err == nil {
initialConfig.SyncInterval = interval
}
}
if syncfile, ok := arguments["<db.sql>"].(string); ok {
initialConfig.SyncFile = syncfile
}
if remoteconn, ok := arguments["<remote>"].(string); ok {
initialConfig.RemoteConn = remoteconn
}
watch, ok := arguments["watch"].(bool)
if watch && initialConfig.UseSSL && (initialConfig.SSLKeyFile == "" || initialConfig.SSLCertFile == "") {
log.Warning("ssl cert file and key file weren't specified, automatically generating")
ssl.GenerateSelfSignedCerts()
homedir := os.Getenv("HOME")
watchdb_dir := path.Join(homedir, ".config", "watchdb")
initialConfig.SSLCertFile = path.Join(watchdb_dir, "watchdb-cert.pem")
initialConfig.SSLKeyFile = path.Join(watchdb_dir, "watchdb-key.pem")
}
return initialConfig
}