Rework module system. Now modules/commands are defined in a single file each, with both the function executed by teh agent and the definition for server-side argument parsing.
This commit is contained in:
38
src/modules/manager.nim
Normal file
38
src/modules/manager.nim
Normal file
@@ -0,0 +1,38 @@
|
||||
import tables, strformat
|
||||
import ../common/[types, utils]
|
||||
|
||||
# Import modules
|
||||
import
|
||||
shell,
|
||||
sleep,
|
||||
filesystem
|
||||
|
||||
type
|
||||
ModuleManager* = object
|
||||
commandsByType*: Table[CommandType, Command]
|
||||
commandsByName*: Table[string, Command]
|
||||
|
||||
var manager: ModuleManager
|
||||
|
||||
proc registerCommands(commands: seq[Command]) {.discardable.} =
|
||||
for cmd in commands:
|
||||
manager.commandsByType[cmd.commandType] = cmd
|
||||
manager.commandsByName[cmd.name] = cmd
|
||||
|
||||
proc loadModules*() =
|
||||
# Register all imported commands
|
||||
registerCommands(shell.commands)
|
||||
registerCommands(sleep.commands)
|
||||
registerCommands(filesystem.commands)
|
||||
|
||||
proc getCommandByType*(cmdType: CommandType): Command =
|
||||
return manager.commandsByType[cmdType]
|
||||
|
||||
proc getCommandByName*(cmdName: string): Command =
|
||||
try:
|
||||
return manager.commandsByName[cmdName]
|
||||
except ValueError:
|
||||
raise newException(ValueError, fmt"The command '{cmdName}' does not exist.")
|
||||
|
||||
proc getAvailableCommands*(): Table[string, Command] =
|
||||
return manager.commandsByName
|
||||
Reference in New Issue
Block a user