Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow environment variables to override config values #1332

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted in the end for this as it doesn't require too much code change and seems to be ok with the now explicit typing.

}

// 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
Original file line number Diff line number Diff line change
@@ -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 = {}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from configstore.

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);