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{}
filter := ""
for {
scanner.Scan()
if !scanner.Scan() {
break
}
line = strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "iface") {
obj.Iface = strings.TrimSpace(strings.TrimPrefix(line, "iface"))
@ -81,7 +83,9 @@ func readConfig(dest string) {
obj := configProxy{}
filter := ""
for {
scanner.Scan()
if !scanner.Scan() {
break
}
line = strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "iface1") {
obj.Iface1 = strings.TrimSpace(strings.TrimPrefix(line, "iface1"))
@ -112,17 +116,26 @@ func readConfig(dest string) {
option = strings.TrimSpace(option)
if modules.ModuleList != nil {
for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == option {
var lines []string
for {
scanner.Scan()
line = strings.TrimSpace(scanner.Text())
lines = append(lines, line)
if strings.HasPrefix(line, "}") {
break
for d := range (*modules.ModuleList[i]).Option {
if (*modules.ModuleList[i]).Option[d].Option == option {
var lines []string
for {
if !scanner.Scan() {
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() {
fmt.Println("PNDPD Version 1.0 - Kioubit 2021")
if modules.ModuleList != nil {
fmt.Print("Loaded Modules: ")
fmt.Print("Loaded Modules: Core ")
for i := range modules.ModuleList {
fmt.Print((*modules.ModuleList[i]).Name + " ")
}
@ -56,9 +56,15 @@ func main() {
readConfig(os.Args[2])
default:
for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == os.Args[1] {
(*modules.ModuleList[i]).CommandLineCallback(os.Args)
return
for d := range (*modules.ModuleList[i]).Option {
if (*modules.ModuleList[i]).Option[d].Option == os.Args[1] {
(*modules.ModuleList[i]).Callback(modules.Callback{
CallbackType: modules.CommandLine,
Option: os.Args[1],
Arguments: os.Args[1:],
})
return
}
}
}
printUsage()
@ -68,9 +74,14 @@ func main() {
}
func printUsage() {
fmt.Println("More options and additional documentation in the example config file")
fmt.Println("Usage:")
fmt.Println("pndpd config <path to file>")
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("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
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) {
// Prints out the contents of the config file that are relevant for this module (that are inside the example{} option)
for _, n := range s {
fmt.Println(n)
}
}
func callback(callback modules.Callback) {
if callback.CallbackType == modules.CommandLine {
// The command registered by the module has been run in the commandline
// "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) {
// Prints out the command line options given to the program if the command starts with "example"
for _, n := range s {
fmt.Println(n)
} else {
// The command registered by the module was found in the config file
// "arguments" contains the lines between the curly braces
fmt.Println("Command: ", callback.Option)
fmt.Println(callback.Arguments)
}
fmt.Println()
}

View File

@ -3,19 +3,33 @@ package modules
var ModuleList []*Module
type Module struct {
Name string
Option string
OptionDescription string
CommandLineCallback func([]string)
ConfigCallback func([]string)
Name string
Option []Option
Callback func(Callback)
}
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{
Name: name,
Option: option,
OptionDescription: description,
CommandLineCallback: commandLineCallback,
ConfigCallback: configCallback,
Name: name,
Option: option,
Callback: Callback,
})
}