| 69d1a72 | | | 1 | import { hubRequest } from "../api.js"; |
| 69d1a72 | | | 2 | import { getRepoSlug } from "../config.js"; |
| 69d1a72 | | | 3 | |
| 69d1a72 | | | 4 | interface LogEntry { |
| 69d1a72 | | | 5 | stream: string; |
| 69d1a72 | | | 6 | content: string; |
| 69d1a72 | | | 7 | created_at: string; |
| 69d1a72 | | | 8 | } |
| 69d1a72 | | | 9 | |
| 69d1a72 | | | 10 | export async function ciLogs(args: string[]) { |
| 69d1a72 | | | 11 | const slug = await getRepoSlug(args); |
| 69d1a72 | | | 12 | |
| 69d1a72 | | | 13 | // Positional args: <run-id> <step-idx> |
| 69d1a72 | | | 14 | const positional = args.filter((a) => !a.startsWith("--") && args[args.indexOf(a) - 1] !== "--repo"); |
| 69d1a72 | | | 15 | const runId = positional[0]; |
| 69d1a72 | | | 16 | const stepIdx = positional[1]; |
| 69d1a72 | | | 17 | |
| 69d1a72 | | | 18 | if (!runId || stepIdx === undefined) { |
| 69d1a72 | | | 19 | console.error("Usage: grove ci logs <run-id> <step-index> [--repo <owner/repo>]"); |
| 69d1a72 | | | 20 | process.exit(1); |
| 69d1a72 | | | 21 | } |
| 69d1a72 | | | 22 | |
| 69d1a72 | | | 23 | const { logs } = await hubRequest<{ logs: LogEntry[] }>( |
| 69d1a72 | | | 24 | `/api/repos/${slug}/canopy/runs/${runId}/logs/${stepIdx}` |
| 69d1a72 | | | 25 | ); |
| 69d1a72 | | | 26 | |
| 69d1a72 | | | 27 | if (logs.length === 0) { |
| 69d1a72 | | | 28 | console.log("No logs for this step."); |
| 69d1a72 | | | 29 | return; |
| 69d1a72 | | | 30 | } |
| 69d1a72 | | | 31 | |
| 69d1a72 | | | 32 | for (const entry of logs) { |
| 69d1a72 | | | 33 | const prefix = entry.stream === "stderr" ? "\x1b[31m" : ""; |
| 69d1a72 | | | 34 | const reset = prefix ? "\x1b[0m" : ""; |
| 69d1a72 | | | 35 | process.stdout.write(`${prefix}${entry.content}${reset}`); |
| 69d1a72 | | | 36 | } |
| 69d1a72 | | | 37 | } |