Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions src/commands/frameworks-backends-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,67 @@ import * as gcp from "../gcp/frameworks";
import { FirebaseError } from "../error";
import { logger } from "../logger";
import { bold } from "colorette";
const Table = require("cli-table");

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:list")
.description("List backends of a Firebase project.")
.option("-l, --location <location>", "App Backend location", "us-central1")
.option("-l, --location <location>", "App Backend location", "-")
.action(async (options: Options) => {
const projectId = needProjectId(options);
const location = options.location as string;
const table = new Table({
head: ["Backend Id", "Repository Name", "URL", "Location", "Created Date", "Updated Date"],
head: TABLE_HEAD,
style: { head: ["green"] },
});

let backendsList;
table.colWidths = COLUMN_LENGTH;
const backendsList: gcp.ListBackendsResponse[] = [];
try {
backendsList = await gcp.listBackends(projectId, location);
for (const backend of backendsList.backends) {
const entry = [
backend.name,
backend.codebase.repository,
backend.uri,
backend.createTime,
backend.updateTime,
];
table.push(entry);
}
const backendsPerRegion = await gcp.listBackends(projectId, location);
backendsList.push(backendsPerRegion);
populateTable(backendsPerRegion, location, table);

logger.info();
logger.info(`Backends for project ${bold(projectId)}`);
logger.info();
logger.info(table.toString());
} catch (err: any) {
throw new FirebaseError(
`Unable to list backends present in project: ${projectId}. Please check the parameters you have provided.`,
`Unable to list backends present for project: ${projectId}. Please check the parameters you have provided.`,
{ original: err }
);
}

return backendsList;
});

function populateTable(backendsLists: gcp.ListBackendsResponse, location: string, table: any) {
for (const backend of backendsLists.backends) {
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);
}
}