From d6421a007e0c12f797ce22857608dea0b2b5f01c Mon Sep 17 00:00:00 2001 From: Kioubit Date: Sat, 25 Dec 2021 13:03:11 -0500 Subject: [PATCH] Better module support --- config.go | 35 ++++++++++++++++++++++++----------- main.go | 21 ++++++++++++++++----- modules/example/example.go | 32 +++++++++++++++++++++----------- modules/modules.go | 36 +++++++++++++++++++++++++----------- 4 files changed, 86 insertions(+), 38 deletions(-) diff --git a/config.go b/config.go index b988e71..00c2ae9 100644 --- a/config.go +++ b/config.go @@ -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) } } } diff --git a/main.go b/main.go index b8d0b21..7fca3c6 100644 --- a/main.go +++ b/main.go @@ -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 ") fmt.Println("pndpd respond ") fmt.Println("pndpd proxy ") - 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) + } + } } diff --git a/modules/example/example.go b/modules/example/example.go index 83b8221..e0ec47e 100644 --- a/modules/example/example.go +++ b/modules/example/example.go @@ -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 ", 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() } diff --git a/modules/modules.go b/modules/modules.go index 0993a9d..36d0be3 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -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, }) }