mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: new cli client to send and receive msg from api
This commit is contained in:
1 parent
e2e585329f
commit
0153eae673
5 files changed
+93
-17
No files matched your search
@@ -1,4 +1,4 @@
|
||||
FROM ubuntu:latest
|
||||
FROM node:latest
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
curl \
|
||||
@@ -11,6 +11,8 @@ USER rudeus
|
||||
|
||||
WORKDIR /home/rudeus
|
||||
|
||||
COPY ./home /home/rudeus/
|
||||
COPY ./server/quests/helloworld/home /home/rudeus/
|
||||
|
||||
COPY ./packages/cli/cli.js /usr/local/bin/cli.js
|
||||
|
||||
CMD [ "/bin/bash" ]
|
||||
@@ -2,26 +2,20 @@
|
||||
preexec() {
|
||||
[ -n "$COMP_LINE" ] && return # do nothing if completing
|
||||
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # do nothing if completing
|
||||
local command="$BASH_COMMAND"
|
||||
local pwd="$PWD"
|
||||
export COMMAND="$BASH_COMMAND"
|
||||
|
||||
if [[ $command =~ ^(vim|top|less|more|nano) ]]; then
|
||||
local payload=$(printf '{"token": "%s", "pwd": "%s", "command": "%s"}' "$TOKEN" "$pwd" "$command")
|
||||
curl -X POST $API_ENDPOINT/api/v1/commands -H 'accept: */*' -H 'Content-Type: application/json' -d "$payload"
|
||||
if [[ $COMMAND =~ ^(vim|top|less|more|nano) ]]; then
|
||||
node /usr/local/bin/cli.js
|
||||
return
|
||||
fi
|
||||
|
||||
# Execute the command, capture output and exit code
|
||||
local output=$( { eval $command 2>&1; echo $? > /tmp/exit_code; })
|
||||
local exit_code=$(cat /tmp/exit_code)
|
||||
export OUTPUT=$( { eval $COMMAND 2>&1; echo $? > /tmp/exit_code; })
|
||||
export EXIT_CODE=$(cat /tmp/exit_code)
|
||||
|
||||
local output_esc=$(echo $output | jq -Rs .)
|
||||
|
||||
# Store command in variable
|
||||
local payload=$(printf '{"token": "%s", "pwd": "%s", "command": "%s", "exit_code": "%s", "output": %s}' "$TOKEN" "$pwd" "$command" "$exit_code" "$output_esc")
|
||||
|
||||
echo $OUTPUT
|
||||
# Send command to the API
|
||||
curl -X POST $API_ENDPOINT/api/v1/commands -H 'accept: */*' -H 'Content-Type: application/json' -d "$payload" || echo $payload
|
||||
node /usr/local/bin/cli.js
|
||||
|
||||
BASH_COMMAND=""
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user