files with local repo volumes

main
Antonio De Lucreziis 2 years ago
parent a2cd57c679
commit 8d951b0e80

@ -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)

@ -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) {

@ -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)