-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (61 loc) · 1.72 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
package main
import (
"komainu/bot"
"komainu/storage"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)
func main() {
if os.Getenv("DEV_MODE") != "" {
log.SetFlags(log.Lshortfile | log.Ltime)
}
cwdOverride := os.Getenv("CWD_OVERRIDE")
if cwdOverride == "" {
exec, err := os.Executable()
if err != nil {
log.Fatalln("Failed to get executable path:", err)
}
path := filepath.Dir(exec)
err = os.Chdir(path)
if err != nil {
log.Fatalln("Failed to change active directory to where the executable lives:", err)
}
log.Printf("Spinning up Komainu in %s\n", path)
} else {
log.Println("CWD_OVERRIDE in effect: " + cwdOverride)
err := os.Chdir(cwdOverride)
if err != nil {
log.Fatalln("Failed to change active directory:", err)
}
}
cfg := storage.GetConfiguration()
if cfg.Logfile != "" {
log.Printf("Using %s for a log file\n", cfg.Logfile)
if logfileHandle, err := os.OpenFile(cfg.Logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640); err == nil {
log.SetOutput(logfileHandle)
defer logfileHandle.Close()
}
} else {
log.Println("No logfile specified: Will just output to STDOUT and hope for the best.")
}
kvs, err := storage.OpenKomainuBolt("data/komainubolt")
if err != nil {
log.Fatalln("Could not open KVS:", err)
}
defer kvs.Close()
log.Println("Preparing to connect to Discord")
state := bot.Connect(&cfg, kvs)
defer state.Close()
WaitForInterrupt()
}
// WaitForInterrupt blocks until a SIGINT, SIGTERM or another OS interrupt is received.
// "Pause until Ctrl+C", basically.
func WaitForInterrupt() {
// Thanks to various Discord Gophers for this very simple stuff.
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGTERM, os.Interrupt)
<-signalCh
}