2021-12-25 09:28:59 -05:00
|
|
|
package modules
|
|
|
|
|
|
|
|
var ModuleList []*Module
|
|
|
|
|
|
|
|
type Module struct {
|
2021-12-27 11:24:21 -05:00
|
|
|
Name string
|
|
|
|
Commands []Command
|
|
|
|
InitCallback func(CallbackInfo)
|
|
|
|
CompleteCallback func()
|
|
|
|
ShutdownCallback func()
|
2021-12-25 09:28:59 -05:00
|
|
|
}
|
|
|
|
|
2021-12-27 11:24:21 -05:00
|
|
|
type Command struct {
|
2021-12-28 06:19:19 -05:00
|
|
|
CommandText string
|
|
|
|
Description string
|
|
|
|
BlockTerminate bool
|
|
|
|
CommandLineEnabled bool
|
|
|
|
ConfigEnabled bool
|
2021-12-25 13:03:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type CallbackType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CommandLine CallbackType = 0
|
|
|
|
Config CallbackType = 1
|
|
|
|
)
|
|
|
|
|
2021-12-27 11:24:21 -05:00
|
|
|
type CallbackInfo struct {
|
2021-12-25 13:03:11 -05:00
|
|
|
CallbackType CallbackType
|
2021-12-27 11:24:21 -05:00
|
|
|
Command Command
|
2021-12-25 13:03:11 -05:00
|
|
|
Arguments []string
|
|
|
|
}
|
|
|
|
|
2021-12-27 11:24:21 -05:00
|
|
|
func RegisterModule(name string, commands []Command, initCallback func(CallbackInfo), CompleteCallback func(), shutdownCallback func()) {
|
2021-12-25 09:28:59 -05:00
|
|
|
ModuleList = append(ModuleList, &Module{
|
2021-12-27 11:24:21 -05:00
|
|
|
Name: name,
|
|
|
|
Commands: commands,
|
|
|
|
InitCallback: initCallback,
|
|
|
|
CompleteCallback: CompleteCallback,
|
|
|
|
ShutdownCallback: shutdownCallback,
|
2021-12-25 09:28:59 -05:00
|
|
|
})
|
|
|
|
}
|
2021-12-27 11:24:21 -05:00
|
|
|
|
2021-12-28 06:19:19 -05:00
|
|
|
func GetCommand(target string, scope CallbackType) (*Module, Command) {
|
2021-12-27 11:24:21 -05:00
|
|
|
for i := range ModuleList {
|
|
|
|
for _, command := range ModuleList[i].Commands {
|
|
|
|
if command.CommandText == target {
|
2021-12-28 06:19:19 -05:00
|
|
|
if scope == CommandLine && command.CommandLineEnabled {
|
|
|
|
return ModuleList[i], command
|
|
|
|
}
|
|
|
|
if scope == Config && command.ConfigEnabled {
|
|
|
|
return ModuleList[i], command
|
|
|
|
}
|
|
|
|
return nil, Command{}
|
2021-12-27 11:24:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, Command{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runningModules []*Module
|
|
|
|
|
|
|
|
func ExecuteInit(module *Module, info CallbackInfo) {
|
|
|
|
if info.Command.BlockTerminate {
|
|
|
|
found := false
|
|
|
|
for _, n := range runningModules {
|
|
|
|
if n == module {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
runningModules = append(runningModules, module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.InitCallback(info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecuteComplete() {
|
|
|
|
for i := range runningModules {
|
|
|
|
(*runningModules[i]).CompleteCallback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShutdownAll() {
|
|
|
|
for i := range runningModules {
|
|
|
|
(*runningModules[i]).ShutdownCallback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExistsBlockingModule() bool {
|
|
|
|
return len(runningModules) != 0
|
|
|
|
}
|