-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (90 loc) · 2.86 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/gamemann/Rust-Auto-Wipe/pkg/debug"
"github.com/gamemann/tmc-servers-engine/internal/Config"
"github.com/gamemann/tmc-servers-engine/internal/Engine"
)
const HELP_MENU = "Help Options\n\t-cfg= --cfg -cfg <path> > Path to config file override.\n\t-l --list > Print out full config.\n\t-v --version > Print out version and exit.\n\t-h --help > Display help menu.\n\n"
const VERSION = "1.0.0"
func main() {
var list bool
var version bool
var help bool
// Setup simple flags (booleans).
flag.BoolVar(&list, "list", false, "Print out config and exit.")
flag.BoolVar(&list, "l", false, "Print out config and exit.")
flag.BoolVar(&version, "version", false, "Print out version and exit.")
flag.BoolVar(&version, "v", false, "Print out version and exit.")
flag.BoolVar(&help, "help", false, "Print out help menu and exit.")
flag.BoolVar(&help, "h", false, "Print out help menu and exit.")
// Look for 'cfg' flag in command line arguments (default path: ./settings.json).
configFile := flag.String("cfg", "settings.json", "Path to config file.")
// Parse flags.
flag.Parse()
// Check for version flag.
if version {
fmt.Print(VERSION)
os.Exit(0)
}
// Check for help flag.
if help {
fmt.Print(HELP_MENU)
os.Exit(0)
}
// Create config struct.
cfg := Config.Config{}
// Set config defaults.
cfg.SetDefaults()
// Attempt to read config.
err := cfg.LoadConfig(*configFile)
// If we have no config, create the file with the defaults.
if err != nil {
// If there's an error and it contains "no such file", try to create the file with defaults.
if strings.Contains(err.Error(), "no such file") {
err = cfg.WriteDefaultsToFile(*configFile)
if err != nil {
fmt.Println("Failed to open config file and cannot create file.")
fmt.Println(err)
os.Exit(1)
}
}
fmt.Println("WARNING - No config file found. Created config file at " + *configFile + " with defaults.")
}
// Check for list flag.
if list {
// Encode config as JSON string.
json_data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(json_data))
os.Exit(0)
}
// Add our engines!
engines, err := Engine.IPS4_GetEngines(&cfg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Loop through each engine.
for i := 0; i < len(engines); i++ {
// We don't use anything other than IPS 4 right now, set to IPS 4.
engines[i].APIName = "IPS4"
// Launch handler for this engine in a separate thread!
go engines[i].Handler(&cfg)
debug.SendDebugMsg("ALL", int(cfg.Debug), 1, "Spawned engine thread (Class Name => "+engines[i].Class+". API Name => "+engines[i].APIName+").")
}
// Signal.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
<-sigc
os.Exit(0)
}