forked from unrealsync/unrealsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
171 lines (142 loc) · 3.72 KB
/
settings.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"strconv"
"strings"
"github.com/glacjay/goini"
"github.com/unrealsync/unrealsync/list"
)
const generalSection = "general_settings"
type Settings struct {
excludes map[string]bool
username string
sudouser string
host string
port int
dir string
remoteBinPath string
os string
batchMode bool
compression bool
sendQueueSizeLimit int64
}
func parseServerSettings(section string, serverSettings map[string]string, excludes map[string]bool) Settings {
var (
port int
sendQueueSizeLimit int
err error
)
if serverSettings["port"] != "" {
port, err = strconv.Atoi(serverSettings["port"])
if err != nil {
fatalLn("Cannot parse 'port' property in [" + section + "] section of " + repoConfigFilename + ": " + err.Error())
}
}
if serverSettings["send-queue-size-limit"] != "" {
sendQueueSizeLimit, err = strconv.Atoi(serverSettings["send-queue-size-limit"])
if err != nil {
fatalLn("Cannot parse 'send-queue-size-limit' property in [" + section + "] section of " + repoConfigFilename + ": " + err.Error())
}
}
localExcludes := make(map[string]bool)
for key, value := range excludes {
localExcludes[key] = value
}
if serverSettings["exclude"] != "" {
for key, value := range parseExcludes(serverSettings["exclude"]) {
localExcludes[key] = value
}
}
host, ok := serverSettings["host"]
if !ok {
host = section
}
if len(sudoUser) > 0 {
serverSettings["sudouser"] = sudoUser
}
batchMode := serverSettings["batchmode"] != "false"
compression := serverSettings["compression"] != "false"
return Settings{
localExcludes,
serverSettings["username"],
serverSettings["sudouser"],
host,
port,
serverSettings["dir"],
serverSettings["remote-bin-path"],
serverSettings["os"],
batchMode,
compression,
int64(sendQueueSizeLimit),
}
}
func parseExcludes(excl string) map[string]bool {
result := make(map[string]bool)
for _, filename := range strings.Split(excl, "|") {
result[filename] = true
}
return result
}
func parseConfig() (servers map[string]Settings, excludes map[string]bool) {
servers = make(map[string]Settings)
excludes = make(map[string]bool)
dict, err := ini.Load(repoConfigFilename)
if err != nil {
fatalLn("Cannot parse client_config file: ", err)
}
general, ok := dict[generalSection]
if !ok {
fatalLn("Section " + generalSection + " of config file " + repoConfigFilename + " is empty")
}
excludes[".unrealsync"] = true
if general["exclude"] != "" {
for key, value := range parseExcludes(general["exclude"]) {
excludes[key] = value
}
}
forceServers := general["servers"]
if forceServersFlag != "" {
forceServers = forceServersFlag
}
delete(dict, generalSection)
for key, serverSettings := range dict {
if key == "" {
if len(serverSettings) > 0 {
progressLn("You should not have top-level settings in " + repoConfigFilename)
}
continue
}
if _, ok := serverSettings["disabled"]; ok {
progressLn("Skipping [" + key + "] as disabled")
continue
}
for generalKey, generalValue := range general {
if serverSettings[generalKey] == "" {
serverSettings[generalKey] = generalValue
}
}
var keys []string
keys, err = list.Expand(key)
if err != nil {
fatalLn(fmt.Sprintf(
"Server name pattern '%s' parse error [config]: %s", key, err,
))
}
for _, k := range keys {
if forceServers != "" {
var res bool
res, err = list.Glob(forceServers, k)
if err != nil {
fatalLn(fmt.Sprintf(
"Server name pattern '%s' parse error [override]: %s", key, err,
))
}
if !res {
continue
}
}
servers[k] = parseServerSettings(k, serverSettings, excludes)
}
}
return
}