dev_arc_aws/backend/src/routes/shellPrompt.ts

32 lines
1.5 KiB
TypeScript
Raw Normal View History

import type { FastifyInstance } from 'fastify'
import { z } from 'zod'
import { withSshClient } from '../ssh/docker.js'
import { PROMPT_PRESETS, checkPromptStatus, installPrompt, isValidPresetId } from '../ssh/shellPrompt.js'
/**
* Shell prompt setup for SSH-managed hosts: lets the Terminal page show a
* preset picker and install Starship + a Nerd Font on demand, instead of
* requiring the user to run a script by hand on every host.
*/
export async function shellPromptRoutes(app: FastifyInstance) {
app.addHook('onRequest', app.authenticate)
app.get('/api/ssh/:integrationId/shell-prompt', async (req, reply) => {
const integrationId = Number((req.params as { integrationId: string }).integrationId)
const result = await withSshClient(integrationId, (client) => checkPromptStatus(client))
if (!result.ok) return reply.code(502).send({ error: result.error })
return { presets: PROMPT_PRESETS, status: result.value }
})
app.post('/api/ssh/:integrationId/shell-prompt', async (req, reply) => {
const integrationId = Number((req.params as { integrationId: string }).integrationId)
const parsed = z.object({ presetId: z.string() }).safeParse(req.body ?? {})
if (!parsed.success || !isValidPresetId(parsed.data.presetId)) {
return reply.code(400).send({ error: 'Invalid preset' })
}
const result = await withSshClient(integrationId, (client) => installPrompt(client, parsed.data.presetId))
if (!result.ok) return reply.code(502).send({ error: result.error })
return { ok: true, log: result.value.log }
})
}