Skip to content

Commit 344e9f2

Browse files
authored
[FDC] Print a warning if the FDC env override didn't match anything (#8677)
1 parent c492e48 commit 344e9f2

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/init/features/dataconnect/index.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Schema, Service, File, Platform } from "../../../dataconnect/types";
1919
import { parseCloudSQLInstanceName, parseServiceName } from "../../../dataconnect/names";
2020
import { logger } from "../../../logger";
2121
import { readTemplateSync } from "../../../templates";
22-
import { logBullet, envOverride } from "../../../utils";
22+
import { logBullet, logWarning, envOverride } from "../../../utils";
2323
import { isBillingEnabled } from "../../../gcp/cloudbilling";
2424
import * as sdk from "./sdk";
2525
import { getPlatformFromFolder } from "../../../dataconnect/fileUtils";
@@ -30,11 +30,6 @@ const SCHEMA_TEMPLATE = readTemplateSync("init/dataconnect/schema.gql");
3030
const QUERIES_TEMPLATE = readTemplateSync("init/dataconnect/queries.gql");
3131
const MUTATIONS_TEMPLATE = readTemplateSync("init/dataconnect/mutations.gql");
3232

33-
// serviceEnvVar is used by Firebase Console to specify which service to import.
34-
// It should be in the form <location>/<serviceId>
35-
// It must be an existing service - if set to anything else, we'll ignore it.
36-
const serviceEnvVar = () => envOverride("FDC_CONNECTOR", "") || envOverride("FDC_SERVICE", "");
37-
3833
export interface RequiredInfo {
3934
serviceId: string;
4035
locationId: string;
@@ -318,23 +313,37 @@ interface serviceAndSchema {
318313
schema?: Schema;
319314
}
320315

316+
/**
317+
* Picks create new service or an existing service from the list of services.
318+
*
319+
* Firebase Console can provide `FDC_CONNECTOR` or `FDC_SERVICE` environment variable.
320+
* If either is present, chooseExistingService try to match it with any existing service
321+
* and short-circuit the prompt.
322+
*
323+
* `FDC_SERVICE` should have the format `<location>/<serviceId>`.
324+
* `FDC_CONNECTOR` should have the same `<location>/<serviceId>/<connectorId>`.
325+
* @param existing
326+
*/
321327
async function chooseExistingService(
322328
existing: serviceAndSchema[],
323329
): Promise<serviceAndSchema | undefined> {
324-
const [serviceLocationFromEnvVar, serviceIdFromEnvVar] = serviceEnvVar().split("/");
325-
const serviceFromEnvVar = existing.find((s) => {
326-
const serviceName = parseServiceName(s.service.name);
327-
return (
328-
serviceName.serviceId === serviceIdFromEnvVar &&
329-
serviceName.location === serviceLocationFromEnvVar
330-
);
331-
});
332-
if (serviceFromEnvVar) {
333-
// FDC_CONNECTOR or FDC_SERVICE env var match an existing service.
334-
logBullet(
335-
`Picking up the existing service ${clc.bold(serviceLocationFromEnvVar + "/" + serviceIdFromEnvVar)}.`,
336-
);
337-
return serviceFromEnvVar;
330+
const serviceEnvVar = envOverride("FDC_CONNECTOR", "") || envOverride("FDC_SERVICE", "");
331+
if (serviceEnvVar) {
332+
const [serviceLocationFromEnvVar, serviceIdFromEnvVar] = serviceEnvVar.split("/");
333+
const serviceFromEnvVar = existing.find((s) => {
334+
const serviceName = parseServiceName(s.service.name);
335+
return (
336+
serviceName.serviceId === serviceIdFromEnvVar &&
337+
serviceName.location === serviceLocationFromEnvVar
338+
);
339+
});
340+
if (serviceFromEnvVar) {
341+
logBullet(
342+
`Picking up the existing service ${clc.bold(serviceLocationFromEnvVar + "/" + serviceIdFromEnvVar)}.`,
343+
);
344+
return serviceFromEnvVar;
345+
}
346+
logWarning(`Unable to pick up an existing service based on FDC_SERVICE=${serviceEnvVar}.`);
338347
}
339348
const choices: Array<{ name: string; value: serviceAndSchema | undefined }> = existing.map(
340349
(s) => {

src/init/features/dataconnect/sdk.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ import {
2626
import { DataConnectEmulator } from "../../../emulator/dataconnectEmulator";
2727
import { FirebaseError } from "../../../error";
2828
import { camelCase, snakeCase, upperFirst } from "lodash";
29-
import { logSuccess, logBullet, promptForDirectory, envOverride } from "../../../utils";
29+
import { logSuccess, logBullet, promptForDirectory, envOverride, logWarning } from "../../../utils";
3030
import { getGlobalDefaultAccount } from "../../../auth";
3131

32-
// connectorEnvVar is used by Firebase Console to specify which connector to setup.
33-
// It should be in the form <connectorId>.
34-
// It's common to provide both FDC_SERVICE and FDC_CONNECTOR environment variables
35-
const connectorEnvVar = () => envOverride("FDC_CONNECTOR", "");
36-
3732
export const FDC_APP_FOLDER = "_FDC_APP_FOLDER";
3833
export type SDKInfo = {
3934
connectorYamlContents: string;
@@ -146,17 +141,31 @@ interface connectorChoice {
146141
value: ConnectorInfo;
147142
}
148143

144+
/**
145+
* Picks an existing connector from those present in the local workspace.
146+
*
147+
* Firebase Console can provide `FDC_CONNECTOR` environment variable.
148+
* If its is present, chooseExistingConnector try to match it with any existing connectors
149+
* and short-circuit the prompt.
150+
*
151+
* `FDC_CONNECTOR` should have the same `<location>/<serviceId>/<connectorId>`.
152+
* @param choices
153+
*/
149154
async function chooseExistingConnector(choices: connectorChoice[]): Promise<ConnectorInfo> {
150155
if (choices.length === 1) {
151156
// Only one connector available, use it.
152157
return choices[0].value;
153158
}
154-
const nameFromEnvVar = connectorEnvVar();
155-
const existingConnector = choices.find((c) => c.name === nameFromEnvVar);
156-
if (existingConnector) {
157-
// FDC_CONNECTOR env var match an existing connector.
158-
logBullet(`Picking up the existing connector ${clc.bold(nameFromEnvVar)}.`);
159-
return existingConnector.value;
159+
const connectorEnvVar = envOverride("FDC_CONNECTOR", "");
160+
if (connectorEnvVar) {
161+
const existingConnector = choices.find((c) => c.name === connectorEnvVar);
162+
if (existingConnector) {
163+
logBullet(`Picking up the existing connector ${clc.bold(connectorEnvVar)}.`);
164+
return existingConnector.value;
165+
}
166+
logWarning(
167+
`Unable to pick up an existing connector based on FDC_CONNECTOR=${connectorEnvVar}.`,
168+
);
160169
}
161170
return await select<ConnectorInfo>({
162171
message: "Which connector do you want set up a generated SDK for?",

0 commit comments

Comments
 (0)