Skip to content

Commit

Permalink
Updated delete command for M2 (#6525)
Browse files Browse the repository at this point in the history
* Updated delete command for M2

* removed unused variable
  • Loading branch information
svnsairam authored Nov 16, 2023
1 parent 4343d35 commit 299e2d9
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions src/commands/frameworks-backends-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,73 @@ import { FirebaseError } from "../error";
import * as gcp from "../gcp/frameworks";
import { promptOnce } from "../prompt";
import * as utils from "../utils";
import { logger } from "../logger";
import { DEFAULT_REGION, ALLOWED_REGIONS } from "../init/features/frameworks/constants";
const Table = require("cli-table");

const COLUMN_LENGTH = 20;
const TABLE_HEAD = [
"Backend Id",
"Repository Name",
"Location",
"URL",
"Created Date",
"Updated Date",
];

export const command = new Command("backends:delete")
.description("Delete a backend from a Firebase project")
.option("-l, --location <location>", "App Backend location", "us-central1")
.option("-s, --backendId <backendId>", "Backend Id", "")
.option("-l, --location <location>", "App Backend location", "")
.option("-s, --backend <backend>", "Backend Id", "")
.withForce()
.action(async (options: Options) => {
const projectId = needProjectId(options);
const location = options.location as string;
const backendId = options.backendId as string;
let location = options.location as string;
const backendId = options.backend as string;
if (!backendId) {
throw new FirebaseError("Backend id can't be empty.");
}

if (!location) {
location = await promptOnce({
name: "region",
type: "list",
default: DEFAULT_REGION,
message: "Please select the region of the backend you'd like to delete:",
choices: ALLOWED_REGIONS,
});
}

const table = new Table({
head: TABLE_HEAD,
style: { head: ["green"] },
});
table.colWidths = COLUMN_LENGTH;

let backend;
try {
backend = await gcp.getBackend(projectId, location, backendId);
populateTable(backend, table);
} catch (err: any) {
throw new FirebaseError(`No backends found with given parameters. Command aborted.`, {
original: err,
});
}

utils.logWarning("You are about to permanently delete the backend:");
logger.info(table.toString());

const confirmDeletion = await promptOnce(
{
type: "confirm",
name: "force",
default: false,
message: "You are about to delete the backend with id: " + backendId + "\n Are you sure?",
message: "Are you sure?",
},
options
);
if (!confirmDeletion) {
throw new FirebaseError("Deletion aborted.");
throw new FirebaseError("Deletion Aborted");
}

try {
Expand All @@ -40,4 +83,27 @@ export const command = new Command("backends:delete")
{ original: err }
);
}

return backend;
});

function populateTable(backend: gcp.Backend, table: any) {
const [location, , backendId] = backend.name.split("/").slice(3, 6);
const entry = [
backendId,
backend.codebase.repository?.split("/").pop(),
location,
backend.uri,
backend.createTime,
backend.updateTime,
];
const newRow = entry.map((name) => {
const maxCellWidth = COLUMN_LENGTH - 2;
const chunks = [];
for (let i = 0; name && i < name.length; i += maxCellWidth) {
chunks.push(name.substring(i, i + maxCellWidth));
}
return chunks.join("\n");
});
table.push(newRow);
}

0 comments on commit 299e2d9

Please sign in to comment.