-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (73 loc) · 1.68 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
package main
import (
"fmt"
"log/slog"
"mansionTent/share"
"mansionTent/tent"
"mansionTent/tower"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
"github.com/lmittmann/tint"
)
func main() {
timer := share.NewPerfTimer()
loadDotEnv()
activateLogger()
// check command-line arguments
main := make(map[string]func())
main["bot"] = tower.RunBot
main["launch"] = tent.RunLauncher
main["dispatch"] = tower.RunDispatcher
var command string
if len(os.Args) > 1 {
command = os.Args[1]
}
f, ok := main[command]
if !ok {
usage()
os.Exit(1)
}
f()
slog.Info("Exiting cleanly", "elapsed", timer)
}
func loadDotEnv() {
// in the tent, mt.env and the binary will be in an unwritable directory, and cwd is for writing all the files
binaryPath, err := os.Executable()
if err != nil {
slog.Error("Error getting binary path", "err", err)
panic(err)
}
envFilePath := filepath.Join(filepath.Dir(binaryPath), "mt.env")
err = godotenv.Load(envFilePath)
if err != nil {
slog.Error("Error loading .env file", "err", err)
panic(err)
}
}
func activateLogger() {
level := slog.LevelInfo
switch strings.ToUpper(os.Getenv("LOG_LEVEL")) {
case "DEBUG":
level = slog.LevelDebug
case "INFO":
level = slog.LevelInfo
case "WARN":
level = slog.LevelWarn
case "ERROR":
level = slog.LevelError
}
// color my world 💖
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{
Level: level,
TimeFormat: "Mon _2 15:04:05",
})))
}
func usage() {
fmt.Printf("Usage: %s <mode>\n", os.Args[0])
fmt.Println("Modes:")
fmt.Println(" bot - Run the bot")
fmt.Println(" launch - Launch the server")
fmt.Println(" dispatch - Dispatch the server")
}