Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c5d8410

Browse files
committedFeb 13, 2025·
Make apphosting:backends:delete prompt users on which backend to delete.
1 parent de9f7a7 commit c5d8410

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed
 

‎src/apphosting/backend.ts

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

432+
/**
433+
* Fetches
434+
*
435+
*
436+
*/
437+
export async function chooseBackend(
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[0];
458+
}
459+
if (force) {
460+
throw new FirebaseError(
461+
`Force cannot be used because multiple backends were found with ID ${backendId}.`
462+
);
463+
}
464+
465+
const backendsByName = new Map<string, apphosting.Backend>();
466+
backends.forEach((backend) =>
467+
backendsByName.set(`${apphosting.parseBackendName(backend.name).id}(${apphosting.parseBackendName(backend.name).location})`, backend),
468+
);
469+
const chosenBackend = await promptOnce({
470+
name: "backend",
471+
type: "list",
472+
message: chooseBackendPrompt,
473+
choices: [...backendsByName.keys()],
474+
});
475+
return backendsByName.get(chosenBackend)!;
476+
}
477+
432478
/**
433479
* Fetches a backend from the server. If there are multiple backends with that name (ie multi-regional backends),
434480
* prompts the user to disambiguate. If the force option is specified and multiple backends have the same name,

‎src/commands/apphosting-backends-delete.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,25 @@ import {
1010
deleteBackendAndPoll,
1111
getBackendForAmbiguousLocation,
1212
getBackendForLocation,
13+
chooseBackend,
1314
} from "../apphosting/backend";
1415
import * as ora from "ora";
1516

1617
export const command = new Command("apphosting:backends:delete <backend>")
1718
.description("delete a Firebase App Hosting backend")
18-
.option("-l, --location <location>", "specify the location of the backend", "-")
1919
.withForce()
2020
.before(apphosting.ensureApiEnabled)
2121
.action(async (backendId: string, options: Options) => {
2222
const projectId = needProjectId(options);
2323
let location = options.location as string;
2424
let backend: apphosting.Backend;
25-
if (location === "-" || location === "") {
26-
backend = await getBackendForAmbiguousLocation(
27-
projectId,
28-
backendId,
29-
"Please select the location of the backend you'd like to delete:",
30-
);
31-
location = apphosting.parseBackendName(backend.name).location;
32-
} else {
33-
backend = await getBackendForLocation(projectId, location, backendId);
34-
}
25+
26+
backend = await chooseBackend(
27+
projectId,
28+
backendId,
29+
"Please select the backend you'd like to delete:",
30+
options.force,
31+
);
3532

3633
utils.logWarning("You are about to permanently delete this backend:");
3734
printBackendsTable([backend]);

0 commit comments

Comments
 (0)
Please sign in to comment.