From 16fc43223984d11227c614f41df45e8f3417643a Mon Sep 17 00:00:00 2001 From: Matthias Ladkau Date: Fri, 14 Aug 2020 15:01:11 +0100 Subject: [PATCH] feat: Allow environment variables to override config values --- src/lib/config.ts | 2 +- src/lib/user-config.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index fa920482cea..8687e39bc9e 100644 --- a/src/lib/config.ts +++ b/src/lib/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 diff --git a/src/lib/user-config.ts b/src/lib/user-config.ts index 32015343bd4..061db6f134e 100644 --- a/src/lib/user-config.ts +++ b/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);