Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions yarn-project/foundation/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ConfigMapping {
description: string;
isBoolean?: boolean;
nested?: Record<string, ConfigMapping>;
fallback?: EnvVar[];
}

export function isBooleanConfigValue<T>(obj: T, key: keyof T): boolean {
Expand All @@ -22,16 +23,28 @@ export function getConfigFromMappings<T>(configMappings: ConfigMappingsType<T>):
const config = {} as T;

for (const key in configMappings) {
const { env, parseEnv, defaultValue: def, nested } = configMappings[key];
const { env, parseEnv, defaultValue, nested, fallback } = configMappings[key];
if (nested) {
(config as any)[key] = getConfigFromMappings(nested);
} else {
let value;
const val = env ? process.env[env] : undefined;

if (val !== undefined) {
(config as any)[key] = parseEnv ? parseEnv(val) : val;
} else if (def !== undefined) {
(config as any)[key] = def;
value = parseEnv ? parseEnv(val) : val;
} else if (fallback && fallback.length > 0) {
// Try each fallback env var in order
for (const fallbackEnv of fallback) {
const fallbackVal = process.env[fallbackEnv];
if (fallbackVal !== undefined) {
value = parseEnv ? parseEnv(fallbackVal) : fallbackVal;
break;
}
}
}

// Assign the found value or default
(config as any)[key] = value !== undefined ? value : defaultValue;
}
}

Expand Down
1 change: 1 addition & 0 deletions yarn-project/sequencer-client/src/publisher/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const getTxSenderConfigMappings: (
description: 'The private key to be used by the publisher.',
parseEnv: (val: string) => (val ? `0x${val.replace('0x', '')}` : NULL_KEY),
defaultValue: NULL_KEY,
fallback: ['VALIDATOR_PRIVATE_KEY'],
},
});

Expand Down