Skip to content
Snippets Groups Projects
Commit 44838b5a authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Added deep env vars

parent c175e0b1
No related branches found
No related tags found
1 merge request!1Master
...@@ -8,7 +8,8 @@ Simple Config Log with HashiCorp Vault support. ...@@ -8,7 +8,8 @@ Simple Config Log with HashiCorp Vault support.
## Installation ## Installation
- `npm config set @br:registry https://it-devops-01:4873` - `npm config set @br:registry https://npm.br-edv.brnet.int`
- `npm config set strict-ssl false`
- `npm install --save @br/config` - `npm install --save @br/config`
## Usage ## Usage
...@@ -25,6 +26,9 @@ It reads from the following sources, performing a deep merge: ...@@ -25,6 +26,9 @@ It reads from the following sources, performing a deep merge:
- config.json - config.json
- config.defaults.json - config.defaults.json
Enviroment Variables can target deep nested settings:
The Setting _setting.deep.key_ can be reached with *SETTING_DEEP_KEY*
## Examples ## Examples
### Only config.defaults.json ### Only config.defaults.json
...@@ -45,7 +49,7 @@ It reads from the following sources, performing a deep merge: ...@@ -45,7 +49,7 @@ It reads from the following sources, performing a deep merge:
(Enviroment set by command to not pollute your machine) (Enviroment set by command to not pollute your machine)
`setting=overwritten-by-env node examples/env/index.js` `SETTING=overwritten-by-env node examples/env/index.js`
## TODO / Missing ## TODO / Missing
......
...@@ -8,17 +8,20 @@ module.exports = function() { ...@@ -8,17 +8,20 @@ module.exports = function() {
let config = {} let config = {}
const iterate = function (obj) { const objectDeepKeys = function (obj) {
for (let index = 0; index < Object.keys(obj).length; index++) { return Object.keys(obj).filter(key => obj[key] instanceof Object).map(key => objectDeepKeys(obj[key]).map(k => `${key}.${k}`)).reduce((x, y) => x.concat(y), Object.keys(obj))
const element = Object.keys(obj)[index]
if (process.env[element]) {
obj[element] = process.env[element]
}
// TODO: encode values here
if (typeof obj[element] === 'object') {
iterate(element)
} }
const set = function(obj, path, value) {
var schema = obj // a moving reference to internal objects within obj
var pList = path.split('.')
var len = pList.length
for(var i = 0; i < len-1; i++) {
var elem = pList[i]
if( !schema[elem] ) schema[elem] = {}
schema = schema[elem]
} }
schema[pList[len-1]] = value
} }
try { try {
...@@ -35,7 +38,14 @@ module.exports = function() { ...@@ -35,7 +38,14 @@ module.exports = function() {
console.log('No File ' + configLocal) console.log('No File ' + configLocal)
} }
iterate(config) let keys = objectDeepKeys(config)
for (let index = 0; index < keys.length; index++) {
const element = keys[index]
const env = process.env[element.toUpperCase().replace('.', '_')]
if (env) {
set(config, element, env)
}
}
return config return config
} }
\ No newline at end of file
...@@ -58,7 +58,8 @@ describe('config', function() { ...@@ -58,7 +58,8 @@ describe('config', function() {
await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults)) await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
await fs.writeFile('config.json', JSON.stringify(jsonLocals)) await fs.writeFile('config.json', JSON.stringify(jsonLocals))
process.env.setting = 'overwritten-by-env' process.env['SETTING'] = 'overwritten-by-env'
process.env['ANOTHER_MORE'] = 'overwritten-by-env2'
let config = new Config() let config = new Config()
...@@ -66,7 +67,7 @@ describe('config', function() { ...@@ -66,7 +67,7 @@ describe('config', function() {
await fs.unlink('config.defaults.json') await fs.unlink('config.defaults.json')
expect(config.setting).toBe('overwritten-by-env') expect(config.setting).toBe('overwritten-by-env')
expect(config.another.more).toBe('stuff') expect(config.another.more).toBe('overwritten-by-env2')
expect(config.another.setting).toBe('avalue') expect(config.another.setting).toBe('avalue')
}) })
}) })
{ {
"name": "config", "name": "config",
"version": "1.2.0", "version": "1.2.3",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
......
{ {
"name": "@br/config", "name": "@br/config",
"version": "1.2.0", "version": "1.2.3",
"description": "Simple Config Log with HashiCorp Vault support", "description": "Simple Config Log with HashiCorp Vault support",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment