Files
kuber/command/down.ts
T
2026-08-28 22:07:40 +07:00

69 lines
1.9 KiB
TypeScript

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 },
});
}
await new Listr([
{
title: "Delete resources",
skip: () =>
resources.length > 0 ? false : "No managed resources to delete",
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();
},
});