|
| 1 | +import { Command } from "@cliffy/command"; |
| 2 | +import { createTrpcClient } from "../auth.ts"; |
| 3 | +import { actionHandler, getApp, getOrg } from "../config.ts"; |
| 4 | +import type { GlobalContext } from "../main.ts"; |
| 5 | +import { |
| 6 | + renderTemporalTimestamp, |
| 7 | + tablePrinter, |
| 8 | + writeJsonResult, |
| 9 | +} from "../util.ts"; |
| 10 | + |
| 11 | +interface RevisionItem { |
| 12 | + id: string; |
| 13 | + status: string; |
| 14 | + created_at: Date; |
| 15 | + updated_at: Date; |
| 16 | + prod: boolean; |
| 17 | + steps: Array<{ step: string }>; |
| 18 | +} |
| 19 | + |
| 20 | +const deploymentStatuses = [ |
| 21 | + "skipped", |
| 22 | + "queued", |
| 23 | + "building", |
| 24 | + "succeeded", |
| 25 | + "failed", |
| 26 | +] as const; |
| 27 | +type DeploymentStatus = typeof deploymentStatuses[number]; |
| 28 | + |
| 29 | +const deploymentsListCommand = new Command<GlobalContext>() |
| 30 | + .description("List deployments (revisions) for an application") |
| 31 | + .option("--org <name:string>", "The name of the organization") |
| 32 | + .option("--app <name:string>", "The name of the application") |
| 33 | + .option( |
| 34 | + "--limit <n:number>", |
| 35 | + "Maximum number of deployments to return (default 20)", |
| 36 | + ) |
| 37 | + .option("--cursor <c:string>", "Pagination cursor from a previous --json run") |
| 38 | + .option( |
| 39 | + "--status <status:string>", |
| 40 | + `Filter by status: one of ${deploymentStatuses.join(", ")}`, |
| 41 | + ) |
| 42 | + .action(actionHandler(async (config, options) => { |
| 43 | + config.noCreate(); |
| 44 | + const org = await getOrg(options, config, options.org); |
| 45 | + const { app } = await getApp(options, config, false, org, options.app); |
| 46 | + const trpcClient = createTrpcClient(options); |
| 47 | + |
| 48 | + // Cliffy widens the option through its option-builder generics; the |
| 49 | + // backend zod-validates and returns a USAGE error if it's not one of |
| 50 | + // the enum values, which the global error envelope surfaces fine. |
| 51 | + const status = options.status as unknown as DeploymentStatus | undefined; |
| 52 | + |
| 53 | + const res = await trpcClient.query("revisions.listByPage", { |
| 54 | + org, |
| 55 | + app, |
| 56 | + cursor: options.cursor, |
| 57 | + limit: options.limit ?? 20, |
| 58 | + status, |
| 59 | + }) as { items: RevisionItem[]; nextCursor: string | null }; |
| 60 | + |
| 61 | + if (options.json) { |
| 62 | + writeJsonResult({ |
| 63 | + items: res.items.map((r) => ({ |
| 64 | + id: r.id, |
| 65 | + status: r.status, |
| 66 | + prod: r.prod, |
| 67 | + createdAt: r.created_at, |
| 68 | + updatedAt: r.updated_at, |
| 69 | + lastStep: r.steps.at(-1)?.step ?? null, |
| 70 | + })), |
| 71 | + nextCursor: res.nextCursor, |
| 72 | + org, |
| 73 | + app, |
| 74 | + }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (res.items.length === 0) { |
| 79 | + console.log("No deployments for this application."); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + tablePrinter( |
| 84 | + ["REVISION", "STATUS", "PROD", "CREATED", "LAST STEP"], |
| 85 | + res.items, |
| 86 | + (r) => [ |
| 87 | + r.id, |
| 88 | + r.status, |
| 89 | + r.prod ? "yes" : "no", |
| 90 | + renderTemporalTimestamp(r.created_at.toISOString()), |
| 91 | + r.steps.at(-1)?.step ?? "—", |
| 92 | + ], |
| 93 | + ); |
| 94 | + |
| 95 | + if (res.nextCursor) { |
| 96 | + console.log(`\nMore results available; pass --cursor ${res.nextCursor}`); |
| 97 | + } |
| 98 | + })); |
| 99 | + |
| 100 | +export const deploymentsCommand = new Command<GlobalContext>() |
| 101 | + .description("Manage deployments (revisions)") |
| 102 | + .action(() => { |
| 103 | + deploymentsCommand.showHelp(); |
| 104 | + }) |
| 105 | + .command("list", deploymentsListCommand) |
| 106 | + .alias("ls"); |
0 commit comments