Build tags for modules

This commit is contained in:
Kioubit 2021-12-28 06:19:19 -05:00
parent ff27eb5141
commit 2e62c0b5d5
7 changed files with 57 additions and 25 deletions

View File

@ -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 {

10
main.go
View File

@ -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 <path to file>")
for i := range modules.ModuleList {
for d := range (*modules.ModuleList[i]).Commands {
if (*modules.ModuleList[i]).Commands[d].CommandLineEnabled {
fmt.Println("pndpd", (*modules.ModuleList[i]).Commands[d].Description)
}
}
}
}

View File

@ -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,
CommandLineEnabled: true,
ConfigEnabled: true,
}, {
CommandText: "command2",
Description: "This is the usage description for command2",
BlockTerminate: false,
CommandLineEnabled: false,
ConfigEnabled: true,
},
}
modules.RegisterModule("Example", commands, initCallback, completeCallback, shutdownCallback)

View File

@ -0,0 +1 @@
package example

View File

@ -14,6 +14,8 @@ type Command struct {
CommandText string
Description string
BlockTerminate bool
CommandLineEnabled bool
ConfigEnabled bool
}
type CallbackType int
@ -39,12 +41,18 @@ 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 {
if scope == CommandLine && command.CommandLineEnabled {
return ModuleList[i], command
}
if scope == Config && command.ConfigEnabled {
return ModuleList[i], command
}
return nil, Command{}
}
}
}
return nil, Command{}

View File

@ -0,0 +1 @@
package userInterface

View File

@ -1,3 +1,6 @@
//go:build !noUserInterface
// +build !noUserInterface
package userInterface
import (
@ -12,14 +15,20 @@ func init() {
CommandText: "proxy",
Description: "proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>",
BlockTerminate: true,
ConfigEnabled: true,
CommandLineEnabled: true,
}, {
CommandText: "responder",
Description: "responder <interface> <optional whitelist of CIDRs separated by a semicolon>",
BlockTerminate: true,
ConfigEnabled: true,
CommandLineEnabled: true,
}, {
CommandText: "modules",
Description: "modules available - list available modules",
BlockTerminate: false,
ConfigEnabled: false,
CommandLineEnabled: true,
}}
modules.RegisterModule("Core", commands, initCallback, completeCallback, shutdownCallback)
}