Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
config
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
libs
config
Commits
44838b5a
Commit
44838b5a
authored
Apr 14, 2020
by
Sigmund, Dominik
Browse files
Options
Downloads
Patches
Plain Diff
Added deep env vars
parent
c175e0b1
No related branches found
No related tags found
1 merge request
!1
Master
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+6
-2
6 additions, 2 deletions
README.md
index.js
+22
-12
22 additions, 12 deletions
index.js
index.test.js
+3
-2
3 additions, 2 deletions
index.test.js
package-lock.json
+1
-1
1 addition, 1 deletion
package-lock.json
package.json
+1
-1
1 addition, 1 deletion
package.json
with
33 additions
and
18 deletions
README.md
+
6
−
2
View file @
44838b5a
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
index.js
+
22
−
12
View file @
44838b5a
...
@@ -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
This diff is collapsed.
Click to expand it.
index.test.js
+
3
−
2
View file @
44838b5a
...
@@ -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
'
)
})
})
})
})
This diff is collapsed.
Click to expand it.
package-lock.json
+
1
−
1
View file @
44838b5a
{
{
"name"
:
"config"
,
"name"
:
"config"
,
"version"
:
"1.2.
0
"
,
"version"
:
"1.2.
3
"
,
"lockfileVersion"
:
1
,
"lockfileVersion"
:
1
,
"requires"
:
true
,
"requires"
:
true
,
"dependencies"
:
{
"dependencies"
:
{
...
...
This diff is collapsed.
Click to expand it.
package.json
+
1
−
1
View file @
44838b5a
{
{
"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"
:
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment