From 2e62c0b5d5882286283a0b6269edeedb6fe1df76 Mon Sep 17 00:00:00 2001 From: Kioubit Date: Tue, 28 Dec 2021 06:19:19 -0500 Subject: [PATCH] Build tags for modules --- config.go | 2 +- main.go | 12 +++++++++--- modules/example/example.go | 21 +++++++++++++------- modules/example/module.go | 1 + modules/modules.go | 18 ++++++++++++----- modules/userInterface/module.go | 1 + modules/userInterface/userInterface.go | 27 +++++++++++++++++--------- 7 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 modules/example/module.go create mode 100644 modules/userInterface/module.go diff --git a/config.go b/config.go index a0eb381..1811f08 100644 --- a/config.go +++ b/config.go @@ -37,7 +37,7 @@ func readConfig(dest string) { if strings.HasSuffix(line, "{") { option := strings.TrimSuffix(strings.TrimSpace(line), "{") option = strings.TrimSpace(option) - module, command := modules.GetCommand(option) + module, command := modules.GetCommand(option, modules.Config) var lines = make([]string, 0) if module != nil { for { diff --git a/main.go b/main.go index 54de121..cc15a51 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,13 @@ import ( "os" "os/signal" "pndpd/modules" - _ "pndpd/modules/userInterface" "syscall" ) +import ( + // Modules + _ "pndpd/modules/example" + _ "pndpd/modules/userInterface" +) // waitForSignal Waits (blocking) for the program to be interrupted by the OS func waitForSignal() { @@ -29,7 +33,7 @@ func main() { case "config": readConfig(os.Args[2]) default: - module, command := modules.GetCommand(os.Args[1]) + module, command := modules.GetCommand(os.Args[1], modules.CommandLine) if module != nil { modules.ExecuteInit(module, modules.CallbackInfo{ CallbackType: modules.CommandLine, @@ -54,7 +58,9 @@ func printUsage() { fmt.Println("pndpd config ") for i := range modules.ModuleList { for d := range (*modules.ModuleList[i]).Commands { - fmt.Println("pndpd", (*modules.ModuleList[i]).Commands[d].Description) + if (*modules.ModuleList[i]).Commands[d].CommandLineEnabled { + fmt.Println("pndpd", (*modules.ModuleList[i]).Commands[d].Description) + } } } } diff --git a/modules/example/example.go b/modules/example/example.go index c6407fc..13d9b25 100644 --- a/modules/example/example.go +++ b/modules/example/example.go @@ -1,3 +1,6 @@ +//go:build mod_example +// +build mod_example + package example import ( @@ -5,16 +8,20 @@ import ( "pndpd/modules" ) -// This is an example module that is not imported by the main program +// This is an example module func init() { commands := []modules.Command{{ - CommandText: "command1", - Description: "This is the usage description for command1", - BlockTerminate: true, + CommandText: "command1", + Description: "This is the usage description for command1", + BlockTerminate: true, + CommandLineEnabled: true, + ConfigEnabled: true, }, { - CommandText: "command2", - Description: "This is the usage description for command2", - BlockTerminate: false, + CommandText: "command2", + Description: "This is the usage description for command2", + BlockTerminate: false, + CommandLineEnabled: false, + ConfigEnabled: true, }, } modules.RegisterModule("Example", commands, initCallback, completeCallback, shutdownCallback) diff --git a/modules/example/module.go b/modules/example/module.go new file mode 100644 index 0000000..f7ec372 --- /dev/null +++ b/modules/example/module.go @@ -0,0 +1 @@ +package example diff --git a/modules/modules.go b/modules/modules.go index 8ae3d60..c1c50ba 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -11,9 +11,11 @@ type Module struct { } type Command struct { - CommandText string - Description string - BlockTerminate bool + CommandText string + Description string + BlockTerminate bool + CommandLineEnabled bool + ConfigEnabled bool } type CallbackType int @@ -39,11 +41,17 @@ func RegisterModule(name string, commands []Command, initCallback func(CallbackI }) } -func GetCommand(target string) (*Module, Command) { +func GetCommand(target string, scope CallbackType) (*Module, Command) { for i := range ModuleList { for _, command := range ModuleList[i].Commands { if command.CommandText == target { - return ModuleList[i], command + if scope == CommandLine && command.CommandLineEnabled { + return ModuleList[i], command + } + if scope == Config && command.ConfigEnabled { + return ModuleList[i], command + } + return nil, Command{} } } } diff --git a/modules/userInterface/module.go b/modules/userInterface/module.go new file mode 100644 index 0000000..2bca25f --- /dev/null +++ b/modules/userInterface/module.go @@ -0,0 +1 @@ +package userInterface diff --git a/modules/userInterface/userInterface.go b/modules/userInterface/userInterface.go index 0a883e7..86db2b8 100644 --- a/modules/userInterface/userInterface.go +++ b/modules/userInterface/userInterface.go @@ -1,3 +1,6 @@ +//go:build !noUserInterface +// +build !noUserInterface + package userInterface import ( @@ -9,17 +12,23 @@ import ( func init() { commands := []modules.Command{{ - CommandText: "proxy", - Description: "proxy ", - BlockTerminate: true, + CommandText: "proxy", + Description: "proxy ", + BlockTerminate: true, + ConfigEnabled: true, + CommandLineEnabled: true, }, { - CommandText: "responder", - Description: "responder ", - BlockTerminate: true, + CommandText: "responder", + Description: "responder ", + BlockTerminate: true, + ConfigEnabled: true, + CommandLineEnabled: true, }, { - CommandText: "modules", - Description: "modules available - list available modules", - BlockTerminate: false, + CommandText: "modules", + Description: "modules available - list available modules", + BlockTerminate: false, + ConfigEnabled: false, + CommandLineEnabled: true, }} modules.RegisterModule("Core", commands, initCallback, completeCallback, shutdownCallback) }