Skip to content

Commit

Permalink
feat: Allow environment variables to override config values
Browse files Browse the repository at this point in the history
  • Loading branch information
mladkau committed Aug 14, 2020
1 parent ec4aa37 commit 16fc432
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/config.ts
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/lib/user-config.ts
@@ -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.