Skip to content

Commit 31296df

Browse files
authored
feat(projects): add project delete command (#60)
1 parent d20f9ff commit 31296df

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

api/projects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const projects = {
55
create: client("/projects").post,
66
get: client("/projects/{id}").get,
77
update: client("/projects/{id}").put,
8-
// delete: client("/projects/{id}").delete,
8+
delete: client("/projects/{id}").delete,
99
};
1010

1111
export const projectSecrets = {

commands/project/delete/mod.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
});

commands/project/mod.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { command } from "../../zcli.ts";
2+
import { create } from "./create/mod.ts";
23
import { get } from "./get/mod.ts";
4+
import { link } from "./link/mod.ts";
35
import { list } from "./list/mod.ts";
4-
import { create } from "./create/mod.ts";
56
import { update } from "./update/mod.ts";
6-
import { link } from "./link/mod.ts";
7+
import { delete_ } from "./delete/mod.ts";
78

89
export const defaultFields = [
910
"id",
@@ -21,6 +22,7 @@ const subCommands: ReturnType<typeof command>[] = [
2122
create,
2223
update,
2324
link,
25+
delete_,
2426
];
2527

2628
export const project = command("project", {

0 commit comments

Comments
 (0)