forked from broderickhyman/albiondata-client
/
config.go
295 lines (253 loc) · 6.65 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package client
import (
"flag"
"io"
"os"
"strconv"
"strings"
"github.com/ao-data/albiondata-client/log"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type config struct {
AllowedWSHosts []string
Debug bool
Trace bool
DebugEvents map[int]bool
DebugEventsString string
DebugEventsBlacklistString string
DebugOperations map[int]bool
DebugOperationsString string
DebugOperationsBlacklistString string
DebugIgnoreDecodingErrors bool
DisableUpload bool
EnableWebsockets bool
ListenDevices string
LogLevel string
LogToFile bool
Minimize bool
Offline bool
OfflinePath string
RecordPath string
PrivateIngestBaseUrls string
PublicIngestBaseUrls string
NoCPULimit bool
PrintVersion bool
}
//config global config data
var ConfigGlobal = &config{
LogLevel: "INFO",
}
func (config *config) SetupFlags() {
config.setupWebsocketFlags()
config.setupDebugFlags()
config.setupCommonFlags()
flag.Parse()
if config.OfflinePath != "" {
config.Offline = true
config.DisableUpload = true
}
if config.DisableUpload {
log.Info("Upload is disabled.")
}
config.setupLogs()
}
func (config *config) setupWebsocketFlags() {
// Setup the config file and parse values
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
// if we cannot find the configuration file, set Websockets to false
if err != nil {
viper.Set("EnableWebsockets", false)
}
config.EnableWebsockets = viper.GetBool("EnableWebsockets")
config.AllowedWSHosts = viper.GetStringSlice("AllowedWebsocketHosts")
}
func (config *config) setupDebugFlags() {
flag.BoolVar(
&config.PrintVersion,
"version",
false,
"Print version, then close.",
)
flag.BoolVar(
&config.Debug,
"debug",
false,
"Enable debug logging.",
)
flag.BoolVar(
&config.Trace,
"trace",
false,
"Enable trace logging. Even more verbose than debug.",
)
flag.StringVar(
&config.DebugEventsString,
"events",
"",
"Whitelist of event IDs to output messages when debugging. Comma separated.",
)
flag.StringVar(
&config.DebugEventsBlacklistString,
"events-ignore",
"",
"Blacklist of event IDs to hide messages when debugging. Comma separated.",
)
flag.StringVar(
&config.DebugOperationsString,
"operations",
"",
"Whitelist of operation IDs to output messages when debugging. Comma separated.",
)
flag.StringVar(
&config.DebugOperationsBlacklistString,
"operations-ignore",
"",
"Blacklist of operation IDs to hide messages when debugging. Comma separated.",
)
flag.BoolVar(
&config.DebugIgnoreDecodingErrors,
"ignore-decode-errors",
false,
"Ignore the decoding errors when debugging",
)
flag.BoolVar(
&config.NoCPULimit,
"no-limit",
false,
"Use all available CPU cores",
)
}
func (config *config) setupCommonFlags() {
flag.BoolVar(
&config.DisableUpload,
"d",
false,
"If specified no attempts will be made to upload data to remote server.",
)
flag.StringVar(
&config.ListenDevices,
"l",
"",
"Listen on this comma separated devices instead of all available",
)
flag.BoolVar(
&config.LogToFile,
"output-file",
false,
"Enable logging to file.",
)
flag.StringVar(
&config.OfflinePath,
"o",
"",
"Parses a local file instead of checking albion ports.",
)
flag.BoolVar(
&config.Minimize,
"minimize",
false,
"Automatically minimize the window.",
)
flag.StringVar(
&config.PublicIngestBaseUrls,
"i",
"http+pow://albion-online-data.com",
"Base URL to send PUBLIC data to, can be 'nats://', 'http://' or 'noop' and can have multiple uploaders. Comma separated.",
)
flag.StringVar(
&config.PrivateIngestBaseUrls,
"p",
"",
"Base URL to send PRIVATE data to, can be 'nats://', 'http://' or 'noop' and can have multiple uploaders. Comma separated.",
)
flag.StringVar(
&config.RecordPath,
"record",
"",
"Enable recording commands to a file for debugging later.",
)
}
func (config *config) setupLogs() {
if config.Debug {
config.LogLevel = "DEBUG"
}
if config.Trace {
config.LogLevel = "TRACE"
}
level, err := logrus.ParseLevel(strings.ToLower(config.LogLevel))
if err != nil {
log.Errorf("Error getting level: %v", err)
}
log.SetLevel(level)
if config.LogToFile {
log.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true, DisableSorting: true, ForceColors: false})
f, err := os.OpenFile("albiondata-client-output.txt", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0755)
if err == nil {
multiWriter := io.MultiWriter(os.Stdout, f)
log.SetOutput(multiWriter)
} else {
log.SetOutput(os.Stdout)
}
} else {
log.SetFormatter(&logrus.TextFormatter{FullTimestamp: true, DisableSorting: true, ForceColors: true})
log.SetOutput(colorable.NewColorableStdout())
}
}
func (config *config) setupDebugEvents() {
config.DebugEvents = make(map[int]bool)
if config.DebugEventsString != "" {
for _, event := range strings.Split(config.DebugEventsString, ",") {
number, err := strconv.Atoi(event)
if err == nil {
config.DebugEvents[number] = true
}
}
}
if config.DebugEventsBlacklistString != "" {
for _, event := range strings.Split(config.DebugEventsBlacklistString, ",") {
number, err := strconv.Atoi(event)
if err == nil {
config.DebugEvents[number] = false
}
}
}
// Looping through map keys is purposefully random by design in Go
for number, shouldDebug := range config.DebugEvents {
verb := "Ignoring"
if shouldDebug {
verb = "Showing"
}
log.Debugf("[%v] event: [%v]%v", verb, number, EventType(number))
}
}
func (config *config) setupDebugOperations() {
config.DebugOperations = make(map[int]bool)
if config.DebugOperationsString != "" {
for _, operation := range strings.Split(config.DebugOperationsString, ",") {
number, err := strconv.Atoi(operation)
if err == nil {
config.DebugOperations[number] = true
}
}
}
if config.DebugOperationsBlacklistString != "" {
for _, operation := range strings.Split(config.DebugOperationsBlacklistString, ",") {
number, err := strconv.Atoi(operation)
if err == nil {
config.DebugOperations[number] = false
}
}
}
// Looping through map keys is purposefully random by design in Go
for number, shouldDebug := range config.DebugOperations {
verb := "Ignoring"
if shouldDebug {
verb = "Showing"
}
log.Debugf("[%v] operation: [%v]%v", verb, number, OperationType(number))
}
}