67 lines
1.7 KiB
TypeScript
67 lines
1.7 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";
|
|
|
|
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)));
|
|
}
|
|
|
|
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();
|
|
},
|
|
});
|