feat: v2
This commit is contained in:
+25
-42
@@ -1,14 +1,28 @@
|
||||
import { defineCommand } from "citty";
|
||||
import { Listr } from "listr2";
|
||||
import { apiRequest, type ApiRequestInit } from "../lib/api";
|
||||
import { ctx } from "../lib/context";
|
||||
import {
|
||||
assertManagedNamespace,
|
||||
deleteResource,
|
||||
listManagedResources,
|
||||
sortResources,
|
||||
} from "../lib/apply";
|
||||
import { listManagedDatabaseResources } from "../lib/database";
|
||||
import { listManagedStorageResources } from "../lib/storage";
|
||||
|
||||
export type DownResult = {
|
||||
full: boolean;
|
||||
retained: Array<{ kind: string; name: string }>;
|
||||
delete: Array<{ kind: string; name: string }>;
|
||||
};
|
||||
export type DownApiRequest = <T>(
|
||||
path: string,
|
||||
init?: ApiRequestInit,
|
||||
) => Promise<T>;
|
||||
|
||||
export function runDown(
|
||||
project: string,
|
||||
full = false,
|
||||
request: DownApiRequest = apiRequest,
|
||||
): Promise<DownResult> {
|
||||
return request<DownResult>(
|
||||
`/workspaces/${encodeURIComponent(project)}/down`,
|
||||
{ method: "POST", json: { full } },
|
||||
);
|
||||
}
|
||||
|
||||
export const down = defineCommand({
|
||||
meta: {
|
||||
@@ -24,43 +38,12 @@ export const down = defineCommand({
|
||||
},
|
||||
async run({ args }) {
|
||||
const { project } = ctx();
|
||||
await assertManagedNamespace(project);
|
||||
const resources = sortResources(await listManagedResources(project)).filter(
|
||||
(resource) =>
|
||||
args.full ||
|
||||
(resource.kind !== "Ingress" &&
|
||||
resource.kind !== "PersistentVolumeClaim"),
|
||||
);
|
||||
|
||||
if (args.full) {
|
||||
resources.push(...(await listManagedDatabaseResources(project)));
|
||||
resources.push(...(await listManagedStorageResources(project)));
|
||||
}
|
||||
|
||||
if (args.full) {
|
||||
resources.push({
|
||||
apiVersion: "v1",
|
||||
kind: "Namespace",
|
||||
metadata: { name: project },
|
||||
});
|
||||
}
|
||||
|
||||
if (resources.length === 0) return;
|
||||
|
||||
await new Listr([
|
||||
{
|
||||
title: "Delete resources",
|
||||
task: (_ctx, task) => {
|
||||
const ordered = sortResources(resources).reverse();
|
||||
|
||||
task.output = `${ordered.length} resources queued`;
|
||||
return task.newListr(
|
||||
ordered.map((resource) => ({
|
||||
title: `${resource.kind} ${resource.metadata?.name}`,
|
||||
task: () => deleteResource(resource),
|
||||
})),
|
||||
{ concurrent: false, exitOnError: true },
|
||||
);
|
||||
task: async (_ctx, task) => {
|
||||
const result = await runDown(project, args.full);
|
||||
task.output = `${result.delete.length} resources queued`;
|
||||
},
|
||||
},
|
||||
]).run();
|
||||
|
||||
Reference in New Issue
Block a user