Skip to content

Commit 762da44

Browse files
committed
Make apphosting:backends:delete prompt users on which backend to delete.
1 parent c8d258d commit 762da44

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

src/apphosting/backend.ts

+53
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,59 @@ export async function getBackendForLocation(
429429
}
430430
}
431431

432+
/**
433+
* Fetches
434+
*
435+
*
436+
*/
437+
export async function chooseBackends(
438+
projectId: string,
439+
backendId: string,
440+
chooseBackendPrompt: string,
441+
force?: boolean,
442+
): Promise<apphosting.Backend[]> {
443+
let { unreachable, backends } = await apphosting.listBackends(projectId, "-");
444+
if (unreachable && unreachable.length !== 0) {
445+
logWarning(
446+
`The following locations are currently unreachable: ${unreachable}.\n` +
447+
"If your backend is in one of these regions, please try again later.",
448+
);
449+
}
450+
backends = backends.filter(
451+
(backend) => apphosting.parseBackendName(backend.name).id === backendId,
452+
);
453+
if (backends.length === 0) {
454+
throw new FirebaseError(`No backend named "${backendId}" found.`);
455+
}
456+
if (backends.length === 1) {
457+
return backends;
458+
}
459+
460+
if (force) {
461+
throw new FirebaseError(
462+
`Force cannot be used because multiple backends were found with ID ${backendId}`,
463+
);
464+
}
465+
const backendsByDisplay = new Map<string, apphosting.Backend>();
466+
backends.forEach((backend) => {
467+
const { location, id } = apphosting.parseBackendName(backend.name);
468+
backendsByDisplay.set(`${id}(${location})`, backend);
469+
});
470+
const chosenBackends = await promptOnce({
471+
name: "backend",
472+
type: "checkbox",
473+
message: chooseBackendPrompt,
474+
choices: Array.from(backendsByDisplay.keys(), (name) => {
475+
return {
476+
checked: false,
477+
name: name,
478+
value: name,
479+
};
480+
}),
481+
});
482+
return Array.from(chosenBackends, (name) => backendsByDisplay.get(name)!);
483+
}
484+
432485
/**
433486
* Fetches a backend from the server. If there are multiple backends with that name (ie multi-regional backends),
434487
* prompts the user to disambiguate. If the force option is specified and multiple backends have the same name,

src/commands/apphosting-backends-delete.ts

+26-33
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,25 @@ import { promptOnce } from "../prompt";
66
import * as utils from "../utils";
77
import * as apphosting from "../gcp/apphosting";
88
import { printBackendsTable } from "./apphosting-backends-list";
9-
import {
10-
deleteBackendAndPoll,
11-
getBackendForAmbiguousLocation,
12-
getBackendForLocation,
13-
} from "../apphosting/backend";
9+
import { deleteBackendAndPoll, chooseBackends } from "../apphosting/backend";
1410
import * as ora from "ora";
1511

1612
export const command = new Command("apphosting:backends:delete <backend>")
1713
.description("delete a Firebase App Hosting backend")
18-
.option("-l, --location <location>", "specify the location of the backend")
1914
.withForce()
2015
.before(apphosting.ensureApiEnabled)
2116
.action(async (backendId: string, options: Options) => {
2217
const projectId = needProjectId(options);
23-
if (options.location !== undefined) {
24-
utils.logWarning("--location is being removed in the next major release.");
25-
}
26-
let location = (options.location as string) ?? "-";
27-
let backend: apphosting.Backend;
28-
if (location === "-" || location === "") {
29-
backend = await getBackendForAmbiguousLocation(
30-
projectId,
31-
backendId,
32-
"Please select the location of the backend you'd like to delete:",
33-
);
34-
location = apphosting.parseBackendName(backend.name).location;
35-
} else {
36-
backend = await getBackendForLocation(projectId, location, backendId);
37-
}
3818

39-
utils.logWarning("You are about to permanently delete this backend:");
40-
printBackendsTable([backend]);
19+
const backends = await chooseBackends(
20+
projectId,
21+
backendId,
22+
"Please select the backends you'd like to delete:",
23+
options.force,
24+
);
25+
26+
utils.logWarning("You are about to permanently delete these backend(s):");
27+
printBackendsTable(backends);
4128

4229
const confirmDeletion = await promptOnce(
4330
{
@@ -52,14 +39,20 @@ export const command = new Command("apphosting:backends:delete <backend>")
5239
return;
5340
}
5441

55-
const spinner = ora("Deleting backend...").start();
56-
try {
57-
await deleteBackendAndPoll(projectId, location, backendId);
58-
spinner.succeed(`Successfully deleted the backend: ${backendId}`);
59-
} catch (err: unknown) {
60-
spinner.stop();
61-
throw new FirebaseError(`Failed to delete backend: ${backendId}.`, {
62-
original: getError(err),
63-
});
64-
}
42+
const spinner = ora("Deleting backend(s)...").start();
43+
44+
backends.forEach(async (b) => {
45+
const { location, id } = apphosting.parseBackendName(b.name);
46+
try {
47+
await deleteBackendAndPoll(projectId, location, id);
48+
spinner.succeed(`Successfully deleted the backend: ${id}(${location})`);
49+
} catch (err: unknown) {
50+
throw new FirebaseError(
51+
`Failed to delete backend: ${id}(${location}). Please retry to delete remaining backends.`,
52+
{
53+
original: getError(err),
54+
},
55+
);
56+
}
57+
});
6558
});

0 commit comments

Comments
 (0)