diff --git a/src/commands/frameworks-backends-get.ts b/src/commands/frameworks-backends-get.ts new file mode 100644 index 00000000000..f7a79359a33 --- /dev/null +++ b/src/commands/frameworks-backends-get.ts @@ -0,0 +1,44 @@ +import { Command } from "../command"; +import { Options } from "../options"; +import { needProjectId } from "../projectUtils"; +import * as gcp from "../gcp/frameworks"; +import { FirebaseError } from "../error"; +import { logger } from "../logger"; +const Table = require("cli-table"); + +export const command = new Command("backends:get") + .description("Get backend details of a Firebase project") + .option("-l, --location ", "App Backend location", "us-central1") + .option("--s, --backendId ", "Backend Id", "") + .action(async (options: Options) => { + const projectId = needProjectId(options); + const location = options.location as string; + const backendId = options.backendId as string; + if (!backendId) { + throw new FirebaseError("Backend id can't be empty."); + } + + let backend; + try { + backend = await gcp.getBackend(projectId, location, backendId); + const table = new Table({ + head: ["Backend Id", "Repository Name", "URL", "Location", "Created Date", "Updated Date"], + style: { head: ["green"] }, + }); + table.push([ + backend.name, + backend.codebase.repository, + backend.uri, + backend.createTime, + backend.updateTime, + ]); + logger.info(table.toString()); + } catch (err: any) { + throw new FirebaseError( + `Failed to get backend: ${backendId}. Please check the parameters you have provided.`, + { original: err } + ); + } + + return backend; + }); diff --git a/src/commands/frameworks-backends-list.ts b/src/commands/frameworks-backends-list.ts new file mode 100644 index 00000000000..c5f72961888 --- /dev/null +++ b/src/commands/frameworks-backends-list.ts @@ -0,0 +1,44 @@ +import { Command } from "../command"; +import { Options } from "../options"; +import { needProjectId } from "../projectUtils"; +import * as gcp from "../gcp/frameworks"; +import { FirebaseError } from "../error"; +import { logger } from "../logger"; +import { bold } from "colorette"; +const Table = require("cli-table"); + +export const command = new Command("backends:list") + .description("List backends of a Firebase project.") + .option("-l, --location ", "App Backend location", "us-central1") + .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"], + style: { head: ["green"] }, + }); + + let backendsList; + 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); + } + logger.info(`Backends for project ${bold(projectId)}`); + 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.`, + { original: err } + ); + } + + return backendsList; + }); diff --git a/src/commands/frameworks-stacks-get.ts b/src/commands/frameworks-stacks-get.ts deleted file mode 100644 index 91059921654..00000000000 --- a/src/commands/frameworks-stacks-get.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Command } from "../command"; -import { Options } from "../options"; -import { needProjectId } from "../projectUtils"; -import * as gcp from "../gcp/frameworks"; -import { FirebaseError } from "../error"; -import { logger } from "../logger"; - -export const command = new Command("stacks:get") - .description("Get stack details of a Firebase project") - .option("-l, --location ", "App Backend location", "us-central1") - .option("--s, --stackId ", "Stack Id", "") - .action(async (options: Options) => { - const projectId = needProjectId(options); - const location = options.location as string; - const stackId = options.stackId as string; - if (!stackId) { - throw new FirebaseError("Stack id can't be empty."); - } - - let stack; - try { - stack = await gcp.getStack(projectId, location, stackId); - /** - * TODO print this in a prettier way. - */ - logger.info(stack); - } catch (err: any) { - throw new FirebaseError( - `Failed to get stack: ${stackId}. Please check the parameters you have provided.`, - { original: err } - ); - } - - return stack; - }); diff --git a/src/commands/frameworks-stacks-list.ts b/src/commands/frameworks-stacks-list.ts deleted file mode 100644 index 61ccd6bbb16..00000000000 --- a/src/commands/frameworks-stacks-list.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Command } from "../command"; -import { Options } from "../options"; -import { needProjectId } from "../projectUtils"; -import * as gcp from "../gcp/frameworks"; -import { FirebaseError } from "../error"; -import { logger } from "../logger"; - -export const command = new Command("stacks:list") - .description("List stacks of a Firebase project.") - .option("-l, --location ", "App Backend location", "us-central1") - .action(async (options: Options) => { - const projectId = needProjectId(options); - const location = options.location as string; - - let stacks; - try { - stacks = await gcp.listStack(projectId, location); - /** - * TODO print this in a prettier way. - */ - logger.info(stacks); - } catch (err: any) { - throw new FirebaseError( - `Unable to list stacks present in project: ${projectId}. Please check the parameters you have provided.`, - { original: err } - ); - } - - return stacks; - }); diff --git a/src/commands/index.ts b/src/commands/index.ts index cf22615a910..cdb65331cb0 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -153,11 +153,11 @@ export function load(client: any): any { } if (experiments.isEnabled("internalframeworks")) { client.frameworks = {}; - client.frameworks.stacks = {}; - client.frameworks.stacks.list = loadCommand("frameworks-stacks-list"); - client.frameworks.stacks.create = loadCommand("frameworks-backends-create"); - client.frameworks.stacks.create = loadCommand("frameworks-stacks-get"); - client.frameworks.stacks.create = loadCommand("frameworks-backends-delete"); + client.frameworks.backends = {}; + client.frameworks.backends.list = loadCommand("frameworks-backends-list"); + client.frameworks.backends.create = loadCommand("frameworks-backends-create"); + client.frameworks.backends.get = loadCommand("frameworks-backends-get"); + client.frameworks.backends.delete = loadCommand("frameworks-backends-delete"); } client.login = loadCommand("login"); client.login.add = loadCommand("login-add"); diff --git a/src/gcp/frameworks.ts b/src/gcp/frameworks.ts index b6d740074e9..0b753f84dbe 100644 --- a/src/gcp/frameworks.ts +++ b/src/gcp/frameworks.ts @@ -81,8 +81,8 @@ export interface Operation { // end oneof result } -export interface ListStacksResponse { - stacks: Stack[]; +export interface ListBackendsResponse { + backends: Stack[]; } /** @@ -104,25 +104,28 @@ export async function createStack( } /** - * Gets stack details. + * Gets backend details. */ -export async function getStack( +export async function getBackend( projectId: string, location: string, - stackId: string + backendId: string ): Promise { - const name = `projects/${projectId}/locations/${location}/backends/${stackId}`; + const name = `projects/${projectId}/locations/${location}/backends/${backendId}`; const res = await client.get(name); return res.body; } /** - * List all stacks present in a project and region. + * List all backends present in a project and region. */ -export async function listStack(projectId: string, location: string): Promise { +export async function listBackends( + projectId: string, + location: string +): Promise { const name = `projects/${projectId}/locations/${location}/backends`; - const res = await client.get(name); + const res = await client.get(name); return res.body; } diff --git a/src/init/features/frameworks/index.ts b/src/init/features/frameworks/index.ts index 52be59193d6..f92b7054290 100644 --- a/src/init/features/frameworks/index.ts +++ b/src/init/features/frameworks/index.ts @@ -113,7 +113,7 @@ export async function getOrCreateStack(projectId: string, setup: any): Promise { - let stack = await gcp.getStack(projectId, location, setup.frameworks.serviceName); + let stack = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); while (stack) { setup.frameworks.serviceName = undefined; await promptOnce( @@ -139,7 +139,7 @@ async function getExistingStack(projectId: string, setup: any, location: string) }, setup.frameworks ); - stack = await gcp.getStack(projectId, location, setup.frameworks.serviceName); + stack = await gcp.getBackend(projectId, location, setup.frameworks.serviceName); setup.frameworks.existingStack = undefined; } diff --git a/src/test/init/frameworks/index.spec.ts b/src/test/init/frameworks/index.spec.ts index 28ea17f0fd3..01096de69cd 100644 --- a/src/test/init/frameworks/index.spec.ts +++ b/src/test/init/frameworks/index.spec.ts @@ -20,10 +20,10 @@ describe("operationsConverter", () => { .stub(poller, "pollOperation") .throws("Unexpected pollOperation call"); createStackStub = sandbox.stub(gcp, "createStack").throws("Unexpected createStack call"); - getStackStub = sandbox.stub(gcp, "getStack").throws("Unexpected getStack call"); + getStackStub = sandbox.stub(gcp, "getBackend").throws("Unexpected getBackend call"); linkGitHubRepositoryStub = sandbox .stub(repo, "linkGitHubRepository") - .throws("Unexpected getStack call"); + .throws("Unexpected getBackend call"); }); afterEach(() => {