feat: change to use Zsh

* show message from server response
* send output and error to server
This commit is contained in:
wancat committed 2023-08-08 20:38:27 +08:00
1 parent 0153eae673
commit 6204bd0b17
10 files changed
+96 -104

No files matched your search

+3 -2
View File
@@ -3,9 +3,10 @@ FROM node:latest
RUN apt-get update && apt-get install -y \
curl \
vim \
zsh \
jq
RUN adduser --disabled-password --gecos '' rudeus
RUN adduser --disabled-password --gecos '' --shell /bin/zsh rudeus
USER rudeus
@@ -15,4 +16,4 @@ COPY ./server/quests/helloworld/home /home/rudeus/
COPY ./packages/cli/cli.js /usr/local/bin/cli.js
CMD [ "/bin/bash" ]
CMD [ "/bin/zsh" ]
-24
View File
@@ -1,24 +0,0 @@
# This function will be called before each command is executed
preexec() {
[ -n "$COMP_LINE" ] && return # do nothing if completing
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # do nothing if completing
export COMMAND="$BASH_COMMAND"
if [[ $COMMAND =~ ^(vim|top|less|more|nano) ]]; then
node /usr/local/bin/cli.js
return
fi
# Execute the command, capture output and exit code
export OUTPUT=$( { eval $COMMAND 2>&1; echo $? > /tmp/exit_code; })
export EXIT_CODE=$(cat /tmp/exit_code)
echo $OUTPUT
# Send command to the API
node /usr/local/bin/cli.js
BASH_COMMAND=""
}
# Install the trap
trap 'preexec' DEBUG
+36
View File
@@ -0,0 +1,36 @@
PROMPT='%n@%m:%~%# '
unsetopt CASE_GLOB
setopt CHASE_LINKS
emulate bash
typeset -A cmd
preexec() {
export CMD_START_TIME=$(date +%s%N) # capture start time
export CMD_NAME=$1 # capture command
# A list of interactive commands
interactive_commands=("vim" "nano" "top" "ssh" "less" "zsh" "bash")
# Check if the command is in the list of interactive commands
if [[ " ${interactive_commands[@]} " =~ " $1 " ]]; then
# If it is, just execute it normally
eval $1
else
# If it's not, execute it and redirect its output to a file
eval $1 > /tmp/cmd.out 2>/tmp/cmd.error
fi
}
precmd() {
export CMD_END_TIME=$(date +%s%N) # capture end time
export CMD_EXIT_CODE=$? # capture exit status
export CMD_OUTPUT_FILE=/tmp/cmd.out # read command output from file
export CMD_ERROR_FILE=/tmp/cmd.error
# Now run your Node.js script with the result as an argument
node /usr/local/bin/cli.js
rm /tmp/cmd.out /tmp/cmd.error 2>/dev/null # cleanup
}
@@ -7,6 +7,11 @@ export async function newCommand(req, res) {
console.log('new command:', req.body)
const { token, command, pwd, exit_code, output } = req.body
if (!command) {
res.status(400).json({ message: 'command is required' })
return
}
let sessionId
try {
const decoded = await verifySessionJWT(token)
@@ -33,5 +38,7 @@ export async function newCommand(req, res) {
await c.save()
res.status(201).end()
res.status(201).json({
responses: ['Hello, world!', 'How are you?'],
})
}
+1 -2
View File
@@ -11,7 +11,6 @@ const containerOptions = {
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: ['/bin/bash'],
OpenStdin: true,
StdinOnce: false,
HostConfig: {
@@ -59,7 +58,7 @@ export async function attachContainer(container, { token }) {
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Cmd: ['/bin/bash'],
Cmd: ['/bin/zsh'],
Tty: true,
Env: [`TOKEN=${token}`, `API_ENDPOINT=http://app:3000`],
})
+5
View File
@@ -17,6 +17,11 @@ export default async function loadAndUpdateQuests() {
const quests = Promise.all(
questNames.map(async (id) => {
// Check if id is directory
const stat = await fs.stat(path.join(questDirectory, id))
if (!stat.isDirectory()) {
return null
}
const fullPath = path.join(questDirectory, id, 'README.md')
try {