|
| 1 | +import { projects } from "../../../api/projects.ts"; |
| 2 | +import { asserts } from "../../../lib/asserts.ts"; |
| 3 | +import { args, command, flag, flags, z } from "../../../zcli.ts"; |
| 4 | +import { select } from "../../../prompts/select.ts"; |
| 5 | +import { dataTable } from "../../../lib/data-table.ts"; |
| 6 | +import { pickJson } from "../../../lib/pick-json.ts"; |
| 7 | +import { loading } from "../../../lib/loading.ts"; |
| 8 | +import { defaultFields } from "../mod.ts"; |
| 9 | + |
| 10 | +/** |
| 11 | + * This variable is automatically generated by `zcli add`. Do not remove this |
| 12 | + * or change its name unless you're no longer using `zcli add`. |
| 13 | + */ |
| 14 | +const subCommands: ReturnType<typeof command>[] = []; |
| 15 | + |
| 16 | +export const delete_ = command("delete", { |
| 17 | + short: "Delete a project.", |
| 18 | + long: ` |
| 19 | + Delete a project by its ID. If you don't provide an ID, this command will |
| 20 | + prompt you for one based on the projects you have access to. |
| 21 | + `, |
| 22 | + commands: subCommands, |
| 23 | + args: args().tuple([z.string().describe("The project ID to delete.")]) |
| 24 | + .optional(), |
| 25 | + flags: flags({ |
| 26 | + fields: flag({ |
| 27 | + short: "The fields to include in the response.", |
| 28 | + aliases: ["F"], |
| 29 | + }).array(z.string()).optional(), |
| 30 | + }), |
| 31 | + // We use command metadata in the `persistentPreRun` function to check if a |
| 32 | + // command requires an API key. If it does, we'll check to see if one is |
| 33 | + // set. If not, we'll throw an error. |
| 34 | + meta: { |
| 35 | + requireApiKey: true, |
| 36 | + }, |
| 37 | +}).run(async function* ({ args, flags }) { |
| 38 | + let [id] = args; |
| 39 | + |
| 40 | + if (!id) { |
| 41 | + const existingProjects = await loading(projects.list({ limit: 50 })); |
| 42 | + asserts(existingProjects.ok, existingProjects); |
| 43 | + |
| 44 | + const selected = await select( |
| 45 | + "Select a project:", |
| 46 | + existingProjects.data.items, |
| 47 | + { |
| 48 | + filter(input, option) { |
| 49 | + return option.name.toLowerCase().startsWith(input); |
| 50 | + }, |
| 51 | + renderOption(option, isSelected) { |
| 52 | + return `${isSelected ? ">" : " "} ${option.name}`; |
| 53 | + }, |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + asserts(selected, "No project selected."); |
| 58 | + id = selected.id; |
| 59 | + } |
| 60 | + |
| 61 | + const result = await loading(projects.delete({ id }), { |
| 62 | + enabled: !flags.json, |
| 63 | + }); |
| 64 | + |
| 65 | + asserts(result.ok, result); |
| 66 | + |
| 67 | + if (!flags.json) { |
| 68 | + for await ( |
| 69 | + const line of dataTable([result.data], flags.fields ?? defaultFields) |
| 70 | + ) { |
| 71 | + yield line; |
| 72 | + } |
| 73 | + } else { |
| 74 | + yield pickJson(result.data, flags.fields); |
| 75 | + } |
| 76 | +}); |
0 commit comments