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, "{") { if strings.HasSuffix(line, "{") {
option := strings.TrimSuffix(strings.TrimSpace(line), "{") option := strings.TrimSuffix(strings.TrimSpace(line), "{")
option = strings.TrimSpace(option) option = strings.TrimSpace(option)
module, command := modules.GetCommand(option) module, command := modules.GetCommand(option, modules.Config)
var lines = make([]string, 0) var lines = make([]string, 0)
if module != nil { if module != nil {
for { for {

12
main.go
View File

@ -5,9 +5,13 @@ import (
"os" "os"
"os/signal" "os/signal"
"pndpd/modules" "pndpd/modules"
_ "pndpd/modules/userInterface"
"syscall" "syscall"
) )
import (
// Modules
_ "pndpd/modules/example"
_ "pndpd/modules/userInterface"
)
// waitForSignal Waits (blocking) for the program to be interrupted by the OS // waitForSignal Waits (blocking) for the program to be interrupted by the OS
func waitForSignal() { func waitForSignal() {
@ -29,7 +33,7 @@ func main() {
case "config": case "config":
readConfig(os.Args[2]) readConfig(os.Args[2])
default: default:
module, command := modules.GetCommand(os.Args[1]) module, command := modules.GetCommand(os.Args[1], modules.CommandLine)
if module != nil { if module != nil {
modules.ExecuteInit(module, modules.CallbackInfo{ modules.ExecuteInit(module, modules.CallbackInfo{
CallbackType: modules.CommandLine, CallbackType: modules.CommandLine,
@ -54,7 +58,9 @@ func printUsage() {
fmt.Println("pndpd config <path to file>") fmt.Println("pndpd config <path to file>")
for i := range modules.ModuleList { for i := range modules.ModuleList {
for d := range (*modules.ModuleList[i]).Commands { 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)
}
} }
} }
} }

View File

@ -1,3 +1,6 @@
//go:build mod_example
// +build mod_example
package example package example
import ( import (
@ -5,16 +8,20 @@ import (
"pndpd/modules" "pndpd/modules"
) )
// This is an example module that is not imported by the main program // This is an example module
func init() { func init() {
commands := []modules.Command{{ commands := []modules.Command{{
CommandText: "command1", CommandText: "command1",
Description: "This is the usage description for command1", Description: "This is the usage description for command1",
BlockTerminate: true, BlockTerminate: true,
CommandLineEnabled: true,
ConfigEnabled: true,
}, { }, {
CommandText: "command2", CommandText: "command2",
Description: "This is the usage description for command2", Description: "This is the usage description for command2",
BlockTerminate: false, BlockTerminate: false,
CommandLineEnabled: false,
ConfigEnabled: true,
}, },
} }
modules.RegisterModule("Example", commands, initCallback, completeCallback, shutdownCallback) modules.RegisterModule("Example", commands, initCallback, completeCallback, shutdownCallback)

View File

@ -0,0 +1 @@
package example

View File

@ -11,9 +11,11 @@ type Module struct {
} }
type Command struct { type Command struct {
CommandText string CommandText string
Description string Description string
BlockTerminate bool BlockTerminate bool
CommandLineEnabled bool
ConfigEnabled bool
} }
type CallbackType int 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 i := range ModuleList {
for _, command := range ModuleList[i].Commands { for _, command := range ModuleList[i].Commands {
if command.CommandText == target { 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{}
} }
} }
} }

View File

@ -0,0 +1 @@
package userInterface

View File

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