Skip to content

Commit

Permalink
feat(projects): add project delete command (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-kwang authored Apr 4, 2024
1 parent d20f9ff commit 31296df
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
76 changes: 76 additions & 0 deletions commands/project/delete/mod.ts
Original file line number Diff line number Diff line change
@@ -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<typeof command>[] = [];

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);
}
});
6 changes: 4 additions & 2 deletions commands/project/mod.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -21,6 +22,7 @@ const subCommands: ReturnType<typeof command>[] = [
create,
update,
link,
delete_,
];

export const project = command("project", {
Expand Down

0 comments on commit 31296df

Please sign in to comment.