From 8d951b0e805dcf0088e311af60089562d1092bf2 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Fri, 20 Sep 2024 02:20:55 +0200 Subject: [PATCH] files with local repo volumes --- server/docker.js | 2 +- server/server.js | 23 ++++++++++++----------- src/client/term.js | 5 ++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/server/docker.js b/server/docker.js index 59df4b7..c370ffe 100644 --- a/server/docker.js +++ b/server/docker.js @@ -67,7 +67,7 @@ export async function createContainerPty( const id = await createContainer(tag) - const container = pty.spawn('docker', ['exec', '-it', id, ...shellCommand]) + const container = pty.spawn('docker', ['exec', '-it', '-w', '/project', id, ...shellCommand]) container.onExit(async e => { onExit?.(e) diff --git a/server/server.js b/server/server.js index 2ddddaa..fe69be6 100644 --- a/server/server.js +++ b/server/server.js @@ -1,7 +1,10 @@ import express from 'express' import { WebSocketServer } from 'ws' import { createContainerPty } from './docker.js' -import { runCommand } from './utils.js' +import { getContainerPath, runCommand } from './utils.js' +import path from 'path' + +import fs from 'fs/promises' const app = express() @@ -9,11 +12,12 @@ app.use(express.json()) app.get('/api/status', c => c.json(42)) -app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => { +app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls/*', async (req, res) => { const uuid = req.params.uuid + const p = path.normalize(req.params[0]) try { - const files = await runCommand('/bin/ls', ['exec', uuid, '/bin/ls']) + const files = await runCommand('/bin/ls', [path.join(getContainerPath('phc', uuid), p)]) res.json(files.trim().split('\n')) } catch (e) { @@ -23,13 +27,12 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => { app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => { const uuid = req.params.uuid - const path = req.params[0] + const p = path.normalize(req.params[0]) - console.log('Getting', path) + console.log('Getting', p) try { - const content = await runCommand('docker', ['exec', uuid, '/bin/cat', `/project/${path}`]) - + const content = await fs.readFile(path.join(getContainerPath('phc', uuid), p), 'utf8') res.json(content) } catch (e) { res.json({ error: e.toString() }) @@ -37,15 +40,13 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => { }) app.put('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => { - const path = req.params[0] + const p = path.normalize(req.params[0]) const content = req.body console.log(content) try { - await runCommand('docker', ['exec', uuid, '/bin/sh', '-c', `cat - > "/project/${path}"`], { - stdin: content, - }) + await fs.writeFile(path.join(getContainerPath('phc', uuid), p), content) res.json('ok') } catch (e) { diff --git a/src/client/term.js b/src/client/term.js index ddc3f43..e444487 100644 --- a/src/client/term.js +++ b/src/client/term.js @@ -26,13 +26,12 @@ let id ws.addEventListener('message', e => { const event = JSON.parse(e.data) + console.log(event) if (event.type === 'pty') { term.write(event.data) } else if (event.type === 'created') { id = event.id - } else { - console.log(event) } }) @@ -45,7 +44,7 @@ ws.addEventListener('close', () => { }) setInterval(async () => { - const req = await fetch(`/api/container/${id}/ls`) + const req = await fetch(`/api/container/${id}/exec/ls/`) const files = await req.json() console.log(files)