-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (184 loc) · 5.96 KB
/
main.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
package main // import "github.com/JaredReisinger/xyzzybot"
import (
"bufio"
"errors"
"flag"
"os"
"os/signal"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/JaredReisinger/xyzzybot/console"
"github.com/JaredReisinger/xyzzybot/fizmo"
"github.com/JaredReisinger/xyzzybot/games"
"github.com/JaredReisinger/xyzzybot/slack"
)
const (
defaultGameDirectory = "/usr/local/games"
defaultWorkingRoot = "/usr/local/var/xyzzybot"
defaultConfigFile = "/usr/local/etc/xyzzybot/config.json"
)
func main() {
logBase := log.StandardLogger()
logBase.Level = log.DebugLevel
logger := logBase.WithField("component", "main")
configParam := AddConfigFlag()
gameDirParam := flag.String("game-dir", "",
"directory to use for games")
workingRootParam := flag.String("working-root", "",
"directory root for dynamic configuration and game saves/state")
tokenParam := flag.String("token", "",
"Slack bot token to use for authentication")
tokenFileParam := flag.String("token-file", "",
"file to read for Slack bot token")
consoleParam := flag.Bool("console", false,
"use console instead of Slack (for debugging/testing)")
flag.Parse()
// if there's no -config flag, we look in a default location
configFile := *configParam
requireConfig := true
if fallback("config file", &configFile, defaultConfigFile, logger) {
requireConfig = false
}
config, err := ParseConfigFile(configFile, logBase)
if err != nil {
if requireConfig {
logger.WithField("file", configFile).WithError(err).Fatal("error parsing config file")
} else {
logger.WithField("file", configFile).WithError(err).Debug("error parsing default config file")
}
}
// override any config values with command-line ones...
overrideParam("game directory", &config.GameDirectory, gameDirParam, logger)
overrideParam("working root", &config.WorkingRoot, workingRootParam, logger)
if overrideParam("token file", &config.BotTokenFile, tokenFileParam, logger) {
// a command-line token file also overrides a token from config
config.BotToken = ""
}
overrideParam("token", &config.BotToken, tokenParam, logger)
// And one final check... if we have both a token and token file, just
// use the token. In this case we also clear out the token file config
// setting so that they can't disagree.
if config.BotTokenFile != "" && config.BotToken != "" {
logger.WithFields(log.Fields{
"tokenFile": config.BotTokenFile,
"token": config.BotToken,
}).Warn("both token and token file available... using the token!")
config.BotTokenFile = ""
}
// If we *still* have a token file, it means we *don't* have a token... so
// we need to read the file. In this case, we *keep* the token file value
// as well, which helps make it clear that the token *came from* the file.
if config.BotTokenFile != "" {
config.BotToken, err = readTokenFile(config.BotTokenFile, logger)
if err != nil {
logger.WithError(err).Fatal("unable to read token from file")
}
}
// Fill in any defaults if they're still not set...
fallback("game directory", &config.GameDirectory, defaultGameDirectory, logger)
fallback("working root", &config.WorkingRoot, defaultWorkingRoot, logger)
// If we don't have a bot token, that's a fatal error...
if config.BotToken == "" {
logger.Fatal("no bot token found")
}
logger.WithField("config", config).Debug("using config")
// Create components...
terpFactory := &fizmo.ExternalProcessFactory{
Logger: logBase,
}
gamesFS := &games.FileSys{
Directory: config.GameDirectory,
Logger: logBase,
}
logger.Info("Starting xyzzybot...")
defer logger.Info("xyzzybot exited")
// wow... slack/console looks interface-able!
if *consoleParam {
consoleConfig := &console.Config{
Logger: logBase,
Games: gamesFS,
WorkingRoot: config.WorkingRoot,
InterpreterFactory: terpFactory,
}
c, err := console.StartConsole(consoleConfig)
if err != nil {
logger.WithError(err).Error("starting console")
return
}
defer c.Disconnect()
} else {
botConfig := &slack.Config{
BotToken: config.BotToken,
Admins: config.Admins,
Logger: logBase,
Games: gamesFS,
WorkingRoot: config.WorkingRoot,
InterpreterFactory: terpFactory,
}
manager, err := slack.StartManager(botConfig)
if err != nil {
logger.WithError(err).Error("starting slack manager")
return
}
defer manager.Disconnect()
}
runUntilSignal(logger)
logger.Info("xyzzybot exiting")
}
func overrideParam(name string, configVal *string, paramVal *string, logger log.FieldLogger) bool {
if *paramVal == "" {
return false
}
if *configVal != "" {
logger.WithFields(log.Fields{
"configVal": *configVal,
"paramVal": *paramVal,
}).Debugf("overriding %s from config", name)
}
*configVal = *paramVal
return true
}
func fallback(name string, val *string, defaultVal string, logger log.FieldLogger) bool {
if *val != "" {
return false
}
logger.WithField("value", defaultVal).Debugf("falling back to default %s", name)
*val = defaultVal
return true
}
func readTokenFile(tokenFile string, logger log.FieldLogger) (token string, err error) {
logger2 := logger.WithField("file", tokenFile)
f, err := os.Open(tokenFile)
if err != nil {
logger2.WithError(err).Error("unable to open token file")
return
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "#") {
token = line
break
}
}
if err = s.Err(); err != nil {
logger2.WithError(err).Error("reading token file")
return
}
if token == "" {
err = errors.New("No token found in token file")
logger2.WithError(err).Error("getting token")
}
return
}
func runUntilSignal(logger log.FieldLogger) {
// wait until signal....
q := make(chan os.Signal, 1)
signal.Notify(q, syscall.SIGHUP, syscall.SIGINT /*syscall.SIGPIPE,*/, syscall.SIGKILL, syscall.SIGTERM)
sig := <-q
logger.WithField("signal", sig).Info("got signal")
// os.Exit(1)
}