Better module support

This commit is contained in:
Kioubit 2021-12-25 13:03:11 -05:00
parent 6948f5a30d
commit d6421a007e
4 changed files with 86 additions and 38 deletions

View File

@ -56,7 +56,9 @@ func readConfig(dest string) {
obj := configResponder{} obj := configResponder{}
filter := "" filter := ""
for { for {
scanner.Scan() if !scanner.Scan() {
break
}
line = strings.TrimSpace(scanner.Text()) line = strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "iface") { if strings.HasPrefix(line, "iface") {
obj.Iface = strings.TrimSpace(strings.TrimPrefix(line, "iface")) obj.Iface = strings.TrimSpace(strings.TrimPrefix(line, "iface"))
@ -81,7 +83,9 @@ func readConfig(dest string) {
obj := configProxy{} obj := configProxy{}
filter := "" filter := ""
for { for {
scanner.Scan() if !scanner.Scan() {
break
}
line = strings.TrimSpace(scanner.Text()) line = strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "iface1") { if strings.HasPrefix(line, "iface1") {
obj.Iface1 = strings.TrimSpace(strings.TrimPrefix(line, "iface1")) obj.Iface1 = strings.TrimSpace(strings.TrimPrefix(line, "iface1"))
@ -112,17 +116,26 @@ func readConfig(dest string) {
option = strings.TrimSpace(option) option = strings.TrimSpace(option)
if modules.ModuleList != nil { if modules.ModuleList != nil {
for i := range modules.ModuleList { for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == option { for d := range (*modules.ModuleList[i]).Option {
var lines []string if (*modules.ModuleList[i]).Option[d].Option == option {
for { var lines []string
scanner.Scan() for {
line = strings.TrimSpace(scanner.Text()) if !scanner.Scan() {
lines = append(lines, line) break
if strings.HasPrefix(line, "}") { }
break line = strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "}") {
break
}
lines = append(lines, line)
} }
(*modules.ModuleList[i]).Callback(modules.Callback{
CallbackType: modules.Config,
Option: option,
Arguments: lines,
})
return
} }
(*modules.ModuleList[i]).ConfigCallback(lines)
} }
} }
} }

21
main.go
View File

@ -20,7 +20,7 @@ func WaitForSignal() {
func main() { func main() {
fmt.Println("PNDPD Version 1.0 - Kioubit 2021") fmt.Println("PNDPD Version 1.0 - Kioubit 2021")
if modules.ModuleList != nil { if modules.ModuleList != nil {
fmt.Print("Loaded Modules: ") fmt.Print("Loaded Modules: Core ")
for i := range modules.ModuleList { for i := range modules.ModuleList {
fmt.Print((*modules.ModuleList[i]).Name + " ") fmt.Print((*modules.ModuleList[i]).Name + " ")
} }
@ -56,9 +56,15 @@ func main() {
readConfig(os.Args[2]) readConfig(os.Args[2])
default: default:
for i := range modules.ModuleList { for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == os.Args[1] { for d := range (*modules.ModuleList[i]).Option {
(*modules.ModuleList[i]).CommandLineCallback(os.Args) if (*modules.ModuleList[i]).Option[d].Option == os.Args[1] {
return (*modules.ModuleList[i]).Callback(modules.Callback{
CallbackType: modules.CommandLine,
Option: os.Args[1],
Arguments: os.Args[1:],
})
return
}
} }
} }
printUsage() printUsage()
@ -68,9 +74,14 @@ func main() {
} }
func printUsage() { func printUsage() {
fmt.Println("More options and additional documentation in the example config file")
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println("pndpd config <path to file>") fmt.Println("pndpd config <path to file>")
fmt.Println("pndpd respond <interface> <optional whitelist of CIDRs separated by a semicolon>") fmt.Println("pndpd respond <interface> <optional whitelist of CIDRs separated by a semicolon>")
fmt.Println("pndpd proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>") fmt.Println("pndpd proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>")
fmt.Println("More options and additional documentation in the example config file") for i := range modules.ModuleList {
for d := range (*modules.ModuleList[i]).Option {
fmt.Println((*modules.ModuleList[i]).Option[d].Description)
}
}
} }

View File

@ -7,19 +7,29 @@ import (
// This is an example module that is not imported by the main program // This is an example module that is not imported by the main program
func init() { func init() {
modules.RegisterModule("Example", "example", "example <parameter 1> <parameter 2>", commandLineRead, configRead) option := []modules.Option{{
Option: "command1",
Description: "This is the usage description for command1",
}, {
Option: "command2",
Description: "This is the usage description for command2",
},
}
modules.RegisterModule("Example", option, callback)
} }
func configRead(s []string) { func callback(callback modules.Callback) {
// Prints out the contents of the config file that are relevant for this module (that are inside the example{} option) if callback.CallbackType == modules.CommandLine {
for _, n := range s { // The command registered by the module has been run in the commandline
fmt.Println(n) // "arguments" contains the os.Args[] passed to the program after the command registered by this module
} fmt.Println("Command: ", callback.Option)
} fmt.Println(callback.Arguments)
func commandLineRead(s []string) { } else {
// Prints out the command line options given to the program if the command starts with "example" // The command registered by the module was found in the config file
for _, n := range s { // "arguments" contains the lines between the curly braces
fmt.Println(n) fmt.Println("Command: ", callback.Option)
fmt.Println(callback.Arguments)
} }
fmt.Println()
} }

View File

@ -3,19 +3,33 @@ package modules
var ModuleList []*Module var ModuleList []*Module
type Module struct { type Module struct {
Name string Name string
Option string Option []Option
OptionDescription string Callback func(Callback)
CommandLineCallback func([]string)
ConfigCallback func([]string)
} }
func RegisterModule(name string, option string, description string, commandLineCallback func([]string), configCallback func([]string)) { type Option struct {
Option string
Description string
}
type CallbackType int
const (
CommandLine CallbackType = 0
Config CallbackType = 1
)
type Callback struct {
CallbackType CallbackType
Option string
Arguments []string
}
func RegisterModule(name string, option []Option, Callback func(Callback)) {
ModuleList = append(ModuleList, &Module{ ModuleList = append(ModuleList, &Module{
Name: name, Name: name,
Option: option, Option: option,
OptionDescription: description, Callback: Callback,
CommandLineCallback: commandLineCallback,
ConfigCallback: configCallback,
}) })
} }