Skip to content
Permalink
Browse files

feat: Allow environment variables to override config values

  • Loading branch information
mladkau committed Aug 14, 2020
1 parent ec4aa37 commit 16fc43223984d11227c614f41df45e8f3417643a
Showing with 15 additions and 2 deletions.
  1. +1 −1 src/lib/config.ts
  2. +14 −1 src/lib/user-config.ts
@@ -52,7 +52,7 @@ if (!config.org && org) {
// invalid (non-numeric) value will fallback to the default
const timeout = userConfig.get('timeout');
if (!config.timeout) {
config.timeout = +timeout ? +timeout : DEFAULT_TIMEOUT;
config.timeout = timeout && +timeout ? +timeout : DEFAULT_TIMEOUT;
}

// this is a bit of an assumption that our web site origin is the same
@@ -1,3 +1,16 @@
const Configstore = require('configstore');
const pkg = require(__dirname + '/../../package.json');
export const config = new Configstore(pkg.name);

class ConfigStoreWithEnvironmentVariables extends Configstore {
constructor(id, defaults = undefined, options = {}) {
super(id, defaults, options);
}

public get(key: string): string | undefined {
const envKey = `SNYK_CFG_${key.replace(/-/g, '_').toUpperCase()}`;
const envValue = process.env[envKey];
return super.has(key) && !envValue ? String(super.get(key)) : envValue;
}
}

export const config = new ConfigStoreWithEnvironmentVariables(pkg.name);

0 comments on commit 16fc432

Please sign in to comment.
You can’t perform that action at this time.