import { defineCommand } from "citty"; import { Listr } from "listr2"; import { ctx } from "../lib/context"; import { assertManagedNamespace, deleteResource, listManagedResources, sortResources, } from "../lib/apply"; import { listManagedDatabaseResources } from "../lib/database"; import { listManagedStorageResources } from "../lib/storage"; export const down = defineCommand({ meta: { name: "down", description: "Delete managed resources except ingress and PVCs", }, args: { full: { type: "boolean", alias: "f", description: "Also delete ingress, PVCs, and the namespace", }, }, 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 }, ); }, }, ]).run(); }, });