Skip to content

Commit 21a9e52

Browse files
Add boolean env var factory (#9994)
* Add boolean env var factory * fixup! Add boolean env var factory * fix formatting
1 parent 9f0c175 commit 21a9e52

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

packages/wrangler/src/environment-variables/factory.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,49 @@ type DeprecatedNames =
4646

4747
type ElementType<A> = A extends readonly (infer T)[] ? T : never;
4848

49+
/**
50+
* Create a function used to access a boolean environment variable. It may return undefined if the variable is not set.
51+
*
52+
* This is not memoized to allow us to change the value at runtime, such as in testing.
53+
*
54+
* The environment variable must be either "true" or "false" (after lowercasing), otherwise it will throw an error.
55+
*/
56+
export function getBooleanEnvironmentVariableFactory(options: {
57+
variableName: VariableNames;
58+
}): () => boolean | undefined;
59+
export function getBooleanEnvironmentVariableFactory(options: {
60+
variableName: VariableNames;
61+
defaultValue: boolean | (() => boolean);
62+
}): () => boolean;
63+
export function getBooleanEnvironmentVariableFactory(options: {
64+
variableName: VariableNames;
65+
defaultValue?: boolean | (() => boolean);
66+
}): () => boolean | undefined {
67+
return () => {
68+
if (
69+
!(options.variableName in process.env) ||
70+
process.env[options.variableName] === undefined
71+
) {
72+
return typeof options.defaultValue === "function"
73+
? options.defaultValue()
74+
: options.defaultValue;
75+
}
76+
77+
switch (process.env[options.variableName]?.toLowerCase()) {
78+
case "true":
79+
return true;
80+
case "false":
81+
return false;
82+
default:
83+
throw new UserError(
84+
`Expected ${options.variableName} to be "true" or "false", but got ${JSON.stringify(
85+
process.env[options.variableName]
86+
)}`
87+
);
88+
}
89+
};
90+
}
91+
4992
/**
5093
* Create a function used to access an environment variable. It may return undefined if the variable is not set.
5194
*

packages/wrangler/src/environment-variables/misc-variables.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import path from "node:path";
22
import { dedent } from "ts-dedent";
33
import { UserError } from "../errors";
44
import { getGlobalWranglerConfigPath } from "../global-wrangler-config-path";
5-
import { getEnvironmentVariableFactory } from "./factory";
5+
import {
6+
getBooleanEnvironmentVariableFactory,
7+
getEnvironmentVariableFactory,
8+
} from "./factory";
69
import type { Config } from "../config";
710

811
/**
@@ -37,9 +40,10 @@ export const getC3CommandFromEnv = getEnvironmentVariableFactory({
3740
/**
3841
* `WRANGLER_SEND_METRICS` can override whether we attempt to send metrics information to Sparrow.
3942
*/
40-
export const getWranglerSendMetricsFromEnv = getEnvironmentVariableFactory({
41-
variableName: "WRANGLER_SEND_METRICS",
42-
});
43+
export const getWranglerSendMetricsFromEnv =
44+
getBooleanEnvironmentVariableFactory({
45+
variableName: "WRANGLER_SEND_METRICS",
46+
});
4347

4448
/**
4549
* Set `WRANGLER_API_ENVIRONMENT` environment variable to "staging" to tell Wrangler to hit the staging APIs rather than production.
@@ -139,10 +143,10 @@ function getStagingSubdomain(): string {
139143
*
140144
* By default we do, since debug logs could be added to GitHub issues and shouldn't include sensitive information.
141145
*/
142-
export const getSanitizeLogs = getEnvironmentVariableFactory({
146+
export const getSanitizeLogs = getBooleanEnvironmentVariableFactory({
143147
variableName: "WRANGLER_LOG_SANITIZE",
144148
defaultValue() {
145-
return "true";
149+
return true;
146150
},
147151
});
148152

packages/wrangler/src/logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ export class Logger {
109109

110110
debug = (...args: unknown[]) => this.doLog("debug", args);
111111
debugWithSanitization = (label: string, ...args: unknown[]) => {
112-
if (getSanitizeLogs() === "false") {
113-
this.doLog("debug", [label, ...args]);
114-
} else {
112+
if (getSanitizeLogs()) {
115113
this.doLog("debug", [
116114
label,
117115
"omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data",
118116
]);
117+
} else {
118+
this.doLog("debug", [label, ...args]);
119119
}
120120
};
121121
info = (...args: unknown[]) => this.doLog("info", args);

packages/wrangler/src/metrics/commands.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ export const telemetryStatusCommand = createCommand({
6161
const savedConfig = readMetricsConfig();
6262
const sendMetricsEnv = getWranglerSendMetricsFromEnv();
6363
if (config.send_metrics !== undefined || sendMetricsEnv !== undefined) {
64-
const resolvedPermission =
65-
sendMetricsEnv !== undefined
66-
? sendMetricsEnv === "true"
67-
: config.send_metrics;
64+
const resolvedPermission = sendMetricsEnv ?? config.send_metrics;
6865
logger.log(
6966
`Status: ${resolvedPermission ? chalk.green("Enabled") : chalk.red("Disabled")} (set by ${sendMetricsEnv !== undefined ? "environment variable" : "wrangler.toml"})\n`
7067
);

packages/wrangler/src/metrics/metrics-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function getMetricsConfig({
6666
const sendMetricsEnv = getWranglerSendMetricsFromEnv();
6767
if (sendMetricsEnv !== undefined) {
6868
return {
69-
enabled: sendMetricsEnv.toLowerCase() === "true",
69+
enabled: sendMetricsEnv,
7070
deviceId,
7171
};
7272
}

0 commit comments

Comments
 (0)