-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
100 lines (90 loc) · 2.23 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
package main
import (
"bufio"
"fmt"
"os"
"pndpd/modules"
"pndpd/pndp"
"strings"
)
func readConfig(dest string) {
file, err := os.Open(dest)
if err != nil {
configFatalError(err, "")
}
var (
currentOption string
blockMap map[string][]string
)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line, _, _ = strings.Cut(line, "//")
line = strings.TrimSpace(line)
if line == "" {
continue
}
if after, found := strings.CutPrefix(line, "debug"); found {
if strings.TrimSpace(after) == "on" {
pndp.EnableDebugLog()
}
continue
}
if option, after, found := strings.Cut(line, "{"); found {
if after != "" {
configFatalError(nil, "Nothing may follow after '{'. A new line must be used")
}
if blockMap != nil {
configFatalError(nil, "A new '{' block was started before the previous one was closed")
}
currentOption = strings.TrimSpace(option)
blockMap = make(map[string][]string)
continue
}
if before, after, found := strings.Cut(line, "}"); found {
if after != "" || before != "" {
configFatalError(nil, "Nothing may precede or follow '}'. A new line must be used")
}
if blockMap == nil {
configFatalError(nil, "Found a '}' tag without a matching '{' tag.")
}
module, command := modules.GetCommand(currentOption, modules.Config)
if module == nil {
configFatalError(nil, "Unknown configuration block: "+currentOption)
}
modules.ExecuteInit(module, modules.CallbackInfo{
CallbackType: modules.Config,
Command: command,
Config: blockMap,
})
blockMap = nil
continue
}
if blockMap != nil {
kv := strings.SplitN(line, " ", 2)
if len(kv) != 2 {
configFatalError(nil, "Key without value")
}
if blockMap[kv[0]] == nil {
blockMap[kv[0]] = make([]string, 0)
}
blockMap[kv[0]] = append(blockMap[kv[0]], kv[1])
}
}
_ = file.Close()
if err := scanner.Err(); err != nil {
configFatalError(err, "")
}
if modules.ExistsBlockingModule() {
modules.ExecuteComplete()
waitForSignal()
modules.ShutdownAll()
}
}
func configFatalError(err error, explanation string) {
fmt.Println("Error reading config file:", explanation)
if err != nil {
fmt.Println(err)
}
os.Exit(1)
}