|
| 1 | +/** |
| 2 | + * List active axons (beta) |
| 3 | + */ |
| 4 | + |
| 5 | +import chalk from "chalk"; |
| 6 | +import { formatTimeAgo } from "../../components/ResourceListView.js"; |
| 7 | +import { listActiveAxons, type Axon } from "../../services/axonService.js"; |
| 8 | +import { output, outputError, parseLimit } from "../../utils/output.js"; |
| 9 | + |
| 10 | +interface ListOptions { |
| 11 | + limit?: string; |
| 12 | + startingAfter?: string; |
| 13 | + output?: string; |
| 14 | +} |
| 15 | + |
| 16 | +const PAGE_SIZE = 100; |
| 17 | + |
| 18 | +function printTable(axons: Axon[]): void { |
| 19 | + if (axons.length === 0) { |
| 20 | + console.log(chalk.dim("No active axons found")); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const COL_ID = 34; |
| 25 | + const COL_NAME = 28; |
| 26 | + const COL_CREATED = 12; |
| 27 | + |
| 28 | + const header = |
| 29 | + "ID".padEnd(COL_ID) + |
| 30 | + " " + |
| 31 | + "NAME".padEnd(COL_NAME) + |
| 32 | + " " + |
| 33 | + "CREATED".padEnd(COL_CREATED); |
| 34 | + console.log(chalk.bold(header)); |
| 35 | + console.log(chalk.dim("─".repeat(header.length))); |
| 36 | + |
| 37 | + for (const axon of axons) { |
| 38 | + const id = |
| 39 | + axon.id.length > COL_ID ? axon.id.slice(0, COL_ID - 1) + "…" : axon.id; |
| 40 | + const nameRaw = axon.name ?? ""; |
| 41 | + const name = |
| 42 | + nameRaw.length > COL_NAME |
| 43 | + ? nameRaw.slice(0, COL_NAME - 1) + "…" |
| 44 | + : nameRaw; |
| 45 | + const created = formatTimeAgo(axon.created_at_ms); |
| 46 | + console.log( |
| 47 | + `${id.padEnd(COL_ID)} ${name.padEnd(COL_NAME)} ${created.padEnd(COL_CREATED)}`, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + console.log(); |
| 52 | + console.log( |
| 53 | + chalk.dim(`${axons.length} axon${axons.length !== 1 ? "s" : ""}`), |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export async function listAxonsCommand(options: ListOptions): Promise<void> { |
| 58 | + try { |
| 59 | + const maxResults = parseLimit(options.limit); |
| 60 | + const format = options.output || "text"; |
| 61 | + |
| 62 | + let axons: Axon[]; |
| 63 | + |
| 64 | + if (options.startingAfter) { |
| 65 | + const pageLimit = maxResults === Infinity ? PAGE_SIZE : maxResults; |
| 66 | + const { axons: page, hasMore } = await listActiveAxons({ |
| 67 | + limit: pageLimit, |
| 68 | + startingAfter: options.startingAfter, |
| 69 | + }); |
| 70 | + axons = page; |
| 71 | + if (format === "text" && hasMore && axons.length > 0) { |
| 72 | + console.log( |
| 73 | + chalk.dim( |
| 74 | + "More results may be available; use --starting-after with the last ID to continue.", |
| 75 | + ), |
| 76 | + ); |
| 77 | + console.log(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + const all: Axon[] = []; |
| 81 | + let cursor: string | undefined; |
| 82 | + while (all.length < maxResults) { |
| 83 | + const remaining = maxResults - all.length; |
| 84 | + const pageLimit = Math.min(PAGE_SIZE, remaining); |
| 85 | + const { axons: page, hasMore } = await listActiveAxons({ |
| 86 | + limit: pageLimit, |
| 87 | + startingAfter: cursor, |
| 88 | + }); |
| 89 | + all.push(...page); |
| 90 | + if (!hasMore || page.length === 0) { |
| 91 | + break; |
| 92 | + } |
| 93 | + cursor = page[page.length - 1].id; |
| 94 | + } |
| 95 | + axons = all; |
| 96 | + } |
| 97 | + |
| 98 | + if (format !== "text") { |
| 99 | + output(axons, { format, defaultFormat: "json" }); |
| 100 | + } else { |
| 101 | + printTable(axons); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + outputError("Failed to list active axons", error); |
| 105 | + } |
| 106 | +} |
0 commit comments