diff --git a/TODO.txt b/TODO.txt index 6fcb212..b43b2b0 100644 --- a/TODO.txt +++ b/TODO.txt @@ -25,7 +25,8 @@ [x] `toes stop ` [x] `toes restart ` [x] `toes open ` -[ ] `toes logs ` +[x] `toes logs ` +[x] `toes logs -f ` [ ] `toes new` [ ] `toes pull` [ ] `toes push` diff --git a/src/cli/index.ts b/src/cli/index.ts index d334077..949b8c7 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1,6 +1,6 @@ import { program } from 'commander' import { join } from 'path' -import type { App } from '@types' +import type { App, LogLine } from '@types' import { APPS_DIR } from '$apps' const HOST = `http://localhost:${process.env.PORT ?? 3000}` @@ -57,6 +57,22 @@ const restartApp = async (app: string) => { await post(`/api/apps/${app}/restart`) } +async function logApp(name: string) { + const logs: LogLine[] | undefined = await get(`/api/apps/${name}/logs`) + if (!logs) { + console.error(`App not found: ${name}`) + return + } + if (logs.length === 0) { + console.log('No logs yet') + return + } + for (const line of logs) { + const time = new Date(line.time).toLocaleTimeString() + console.log(`${time} ${line.text}`) + } +} + async function openApp(name: string) { const app: App | undefined = await get(`/api/apps/${name}`) if (!app) { @@ -99,6 +115,17 @@ program .argument('', 'app name') .action(restartApp) +program + .command('logs') + .description('Show logs for an app') + .argument('', 'app name') + .action(logApp) + +program + .command('log', { hidden: true }) + .argument('', 'app name') + .action(logApp) + program .command('open') .description('Open an app in browser') diff --git a/src/server/index.tsx b/src/server/index.tsx index 9220597..04b3f75 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -70,6 +70,16 @@ app.get('/api/apps/:app', c => { return c.json(convert(app)) }) +app.get('/api/apps/:app/logs', c => { + const appName = c.req.param('app') + if (!appName) return c.json({ error: 'App not found' }, 404) + + const app = allApps().find(a => a.name === appName) + if (!app) return c.json({ error: 'App not found' }, 404) + + return c.json(app.logs ?? []) +}) + app.post('/api/apps/:app/start', c => { const appName = c.req.param('app') if (!appName) return c.json({ error: 'App not found' }, 404)