diff --git a/api/projects.ts b/api/projects.ts index e77ee8b..fa77b2c 100644 --- a/api/projects.ts +++ b/api/projects.ts @@ -5,7 +5,7 @@ export const projects = { create: client("/projects").post, get: client("/projects/{id}").get, update: client("/projects/{id}").put, - // delete: client("/projects/{id}").delete, + delete: client("/projects/{id}").delete, }; export const projectSecrets = { diff --git a/commands/project/delete/mod.ts b/commands/project/delete/mod.ts new file mode 100644 index 0000000..fd16b12 --- /dev/null +++ b/commands/project/delete/mod.ts @@ -0,0 +1,76 @@ +import { projects } from "../../../api/projects.ts"; +import { asserts } from "../../../lib/asserts.ts"; +import { args, command, flag, flags, z } from "../../../zcli.ts"; +import { select } from "../../../prompts/select.ts"; +import { dataTable } from "../../../lib/data-table.ts"; +import { pickJson } from "../../../lib/pick-json.ts"; +import { loading } from "../../../lib/loading.ts"; +import { defaultFields } from "../mod.ts"; + +/** + * This variable is automatically generated by `zcli add`. Do not remove this + * or change its name unless you're no longer using `zcli add`. + */ +const subCommands: ReturnType[] = []; + +export const delete_ = command("delete", { + short: "Delete a project.", + long: ` + Delete a project by its ID. If you don't provide an ID, this command will + prompt you for one based on the projects you have access to. + `, + commands: subCommands, + args: args().tuple([z.string().describe("The project ID to delete.")]) + .optional(), + flags: flags({ + fields: flag({ + short: "The fields to include in the response.", + aliases: ["F"], + }).array(z.string()).optional(), + }), + // We use command metadata in the `persistentPreRun` function to check if a + // command requires an API key. If it does, we'll check to see if one is + // set. If not, we'll throw an error. + meta: { + requireApiKey: true, + }, +}).run(async function* ({ args, flags }) { + let [id] = args; + + if (!id) { + const existingProjects = await loading(projects.list({ limit: 50 })); + asserts(existingProjects.ok, existingProjects); + + const selected = await select( + "Select a project:", + existingProjects.data.items, + { + filter(input, option) { + return option.name.toLowerCase().startsWith(input); + }, + renderOption(option, isSelected) { + return `${isSelected ? ">" : " "} ${option.name}`; + }, + }, + ); + + asserts(selected, "No project selected."); + id = selected.id; + } + + const result = await loading(projects.delete({ id }), { + enabled: !flags.json, + }); + + asserts(result.ok, result); + + if (!flags.json) { + for await ( + const line of dataTable([result.data], flags.fields ?? defaultFields) + ) { + yield line; + } + } else { + yield pickJson(result.data, flags.fields); + } +}); diff --git a/commands/project/mod.ts b/commands/project/mod.ts index 0725618..decc90e 100644 --- a/commands/project/mod.ts +++ b/commands/project/mod.ts @@ -1,9 +1,10 @@ import { command } from "../../zcli.ts"; +import { create } from "./create/mod.ts"; import { get } from "./get/mod.ts"; +import { link } from "./link/mod.ts"; import { list } from "./list/mod.ts"; -import { create } from "./create/mod.ts"; import { update } from "./update/mod.ts"; -import { link } from "./link/mod.ts"; +import { delete_ } from "./delete/mod.ts"; export const defaultFields = [ "id", @@ -21,6 +22,7 @@ const subCommands: ReturnType[] = [ create, update, link, + delete_, ]; export const project = command("project", {