Pndpd/config.go

74 lines
1.4 KiB
Go
Raw Normal View History

2021-12-21 06:42:30 -05:00
package main
import (
"bufio"
"fmt"
2021-12-21 06:42:30 -05:00
"os"
2021-12-25 09:28:59 -05:00
"pndpd/modules"
2021-12-22 07:01:30 -05:00
"pndpd/pndp"
2021-12-21 06:42:30 -05:00
"strings"
)
func readConfig(dest string) {
file, err := os.Open(dest)
if err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
2021-12-21 06:42:30 -05:00
}
2021-12-25 08:21:07 -05:00
defer func(file *os.File) {
_ = file.Close()
}(file)
2021-12-21 06:42:30 -05:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
2021-12-25 09:28:59 -05:00
if strings.HasPrefix(line, "//") || strings.TrimSpace(line) == "" {
2021-12-21 06:42:30 -05:00
continue
}
if strings.HasPrefix(line, "debug") {
if strings.Contains(line, "on") {
pndp.GlobalDebug = true
fmt.Println("DEBUG ON")
2021-12-21 06:42:30 -05:00
}
continue
}
2021-12-25 09:28:59 -05:00
if strings.HasSuffix(line, "{") {
2021-12-25 09:28:59 -05:00
option := strings.TrimSuffix(strings.TrimSpace(line), "{")
option = strings.TrimSpace(option)
2021-12-28 06:19:19 -05:00
module, command := modules.GetCommand(option, modules.Config)
var lines = make([]string, 0)
if module != nil {
for {
if !scanner.Scan() {
break
2021-12-25 09:28:59 -05:00
}
line := strings.TrimSpace(scanner.Text())
if strings.Contains(line, "}") {
break
}
lines = append(lines, line)
2021-12-25 09:28:59 -05:00
}
modules.ExecuteInit(module, modules.CallbackInfo{
CallbackType: modules.Config,
Command: command,
Arguments: lines,
})
2021-12-25 09:28:59 -05:00
}
2021-12-21 06:42:30 -05:00
}
}
if modules.ExistsBlockingModule() {
modules.ExecuteComplete()
waitForSignal()
modules.ShutdownAll()
}
if err := scanner.Err(); err != nil {
panic(err)
}
2021-12-21 06:42:30 -05:00
}