Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projects): add project delete command #60

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading