|
| 1 | +import { withConfig } from "../config"; |
| 2 | +import { logger } from "../logger"; |
| 3 | +import { requireAuth } from "../user"; |
| 4 | +import { asJson } from "./options"; |
| 5 | +import { listCatalogEntries, truncate } from "./utils"; |
| 6 | +import type { |
| 7 | + CommonYargsArgv, |
| 8 | + StrictYargsOptionsToInterface, |
| 9 | +} from "../yargs-types"; |
| 10 | + |
| 11 | +export function options(yargs: CommonYargsArgv) { |
| 12 | + return asJson(yargs); |
| 13 | +} |
| 14 | + |
| 15 | +function truncateDescription( |
| 16 | + description: string | undefined, |
| 17 | + alreadyUsed: number |
| 18 | +): string { |
| 19 | + if (description === undefined || description === null) { |
| 20 | + return ""; |
| 21 | + } |
| 22 | + |
| 23 | + if (process.stdout.columns === undefined) { |
| 24 | + return truncate(description, 100); |
| 25 | + } |
| 26 | + |
| 27 | + return truncate(description, process.stdout.columns - alreadyUsed); |
| 28 | +} |
| 29 | + |
| 30 | +type HandlerOptions = StrictYargsOptionsToInterface<typeof options>; |
| 31 | +export const handler = withConfig<HandlerOptions>( |
| 32 | + async ({ json, config }): Promise<void> => { |
| 33 | + const accountId = await requireAuth(config); |
| 34 | + const entries = await listCatalogEntries(accountId); |
| 35 | + |
| 36 | + if (json) { |
| 37 | + logger.log(JSON.stringify(entries, null, 2)); |
| 38 | + } else { |
| 39 | + logger.table( |
| 40 | + entries.map((entry) => ({ |
| 41 | + model: entry.id, |
| 42 | + name: entry.name, |
| 43 | + description: truncateDescription( |
| 44 | + entry.description, |
| 45 | + entry.id.length + |
| 46 | + entry.name.length + |
| 47 | + (entry.task ? entry.task.name.length : 0) + |
| 48 | + 10 |
| 49 | + ), |
| 50 | + task: entry.task ? entry.task.name : "", |
| 51 | + })) |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | +); |
0 commit comments