-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
108 lines (100 loc) · 2.32 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
package main
import (
"io/ioutil"
"log"
"time"
"github.com/BurntSushi/toml"
)
type config struct {
Event string
Password string
PlayTime string
Timezone string
DiscordHook string
Enforce bool
Timeout int
Admin []adminData
Image []imageData
Team []teamData
Alternate alternateData
}
func readConfig(conf *config) {
fileContent, err := ioutil.ReadFile("./sarpedon.conf")
if err != nil {
log.Fatalln("Configuration file (./sarpedon.conf) not found:", err)
}
if _, err := toml.Decode(string(fileContent), &conf); err != nil {
log.Fatalln(err)
}
}
func checkConfig() {
if sarpConfig.Password == "" {
log.Fatalln("No password provided!")
}
if sarpConfig.Admin == nil {
log.Fatalln("No admin account(s) provided!")
}
if sarpConfig.Image == nil {
log.Fatalln("No images provided!")
}
if sarpConfig.Timezone == "" {
log.Fatalln("No timezone provided!")
}
if sarpConfig.PlayTime != "" {
if _, err := time.ParseDuration(sarpConfig.PlayTime); err != nil {
log.Fatalln("Invalid duration for playtime: " + err.Error())
}
}
if sarpConfig.Timeout == 0 {
// Default timeout (in seconds)
sarpConfig.Timeout = 15
} else if sarpConfig.Timeout < 0 {
// If a negative value, set no timeout
sarpConfig.Timeout = 0
}
for _, image := range sarpConfig.Image {
if image.Name == "" {
log.Fatalln("Image name is empty:", image)
}
matches := 0
var dupeImage imageData
for _, imageDupe := range sarpConfig.Image {
if image.Name == imageDupe.Name {
dupeImage = imageDupe
matches++
}
}
if matches > 1 {
log.Fatalln("Duplicate image details found:", image, dupeImage)
}
}
for _, team := range sarpConfig.Team {
if team.ID == "" {
log.Fatalln("Team id is empty:", team)
}
if team.Alias == "" {
log.Fatalln("Team alias is empty:", team)
}
matches := 0
var dupeTeam teamData
for _, teamDupe := range sarpConfig.Team {
if team.ID == teamDupe.ID {
dupeTeam = teamDupe
matches++
}
if team.Alias == teamDupe.Alias {
dupeTeam = teamDupe
matches++
}
if matches > 2 {
log.Fatalln("Duplicate team details found:", team, dupeTeam)
}
}
}
if alternateCompletion {
sarpConfig.Alternate.Map = make(map[string]bool)
for _, ID := range sarpConfig.Alternate.Data {
sarpConfig.Alternate.Map[ID] = true
}
}
}