Skip to content

Commit

Permalink
Merge pull request #8 from boxboat/feature/array-options
Browse files Browse the repository at this point in the history
Add array merging options
  • Loading branch information
caleblloyd authored Feb 10, 2021
2 parents 47238ea + 4416086 commit ca18253
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
38 changes: 32 additions & 6 deletions config-merge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const glob = require('glob')
const merge = require('lodash.merge')
const mergeWith = require('lodash.mergewith')
const path = require('path')
const YAML = require('yaml')
const toml = require('toml-j0.4')
Expand All @@ -20,6 +20,7 @@ const envReserved = new Set(["_", "SHLVL"])
// prints the help message
function printHelp() {
console.error("boxboat/config-merge [flags] file1 [file2] ... [fileN]")
console.error("-a, --array merge|overwrite|concat whether to merge, overwrite, or concatenate arrays. defaults to merge")
console.error("-f, --format json|toml|yaml whether to output json, toml, or yaml. defaults to yaml")
console.error("-h --help print the help message")
console.error(" files ending in .env and .sh will be sourced and used for environment variable substitution")
Expand Down Expand Up @@ -57,6 +58,7 @@ function readFileAndSubEnv(file) {
let args = process.argv.slice(2)
let processPositional = true
let setFlag = null
let array = 'merge'
let format = 'yaml'
let obj = {}

Expand All @@ -76,7 +78,16 @@ for (let arg of args) {
printHelp()
process.exit(0)
}
if (setFlag == "f") {
if (setFlag == "a") {
if (arg == "merge" || arg == "overwrite" || arg == "concat") {
array = arg
setFlag = null
} else {
console.error(`Array should be "merge", "overwrite", or "concat", invalid: ${arg}`)
printHelp()
process.exit(1)
}
} else if (setFlag == "f") {
if (arg == "json" || arg == "toml" || arg == "yaml") {
format = arg
setFlag = null
Expand All @@ -85,10 +96,13 @@ for (let arg of args) {
printHelp()
process.exit(1)
}
} else if (arg == "-a" || arg == "--array") {
setFlag = "a"
}
else if (arg == "-f" || arg == "--foramt") {
setFlag = "f"
} else {
}
else {
break
}
positionalArgCount++
Expand All @@ -110,6 +124,18 @@ for (let arg of args) {
}
}

// merge customizer
function customizer(objValue, srcValue) {
if (Array.isArray(objValue)) {
if (array == "overwrite") {
return srcValue
} else if (array == "concat") {
return objValue.concat(srcValue)
}
}
return undefined
}

// process files
for (arg of globArgs) {
// check that file exists
Expand Down Expand Up @@ -152,13 +178,13 @@ for (arg of globArgs) {
}
}
else if (arg.match(mergeJsonRe)) {
merge(obj, JSON.parse(readFileAndSubEnv(arg)))
mergeWith(obj, JSON.parse(readFileAndSubEnv(arg)), customizer)
}
else if (arg.match(mergeTomlRe)) {
merge(obj, toml.parse(readFileAndSubEnv(arg)))
mergeWith(obj, toml.parse(readFileAndSubEnv(arg)), customizer)
}
else if (arg.match(mergeYamlRe)) {
merge(obj, YAML.parse(readFileAndSubEnv(arg)))
mergeWith(obj, YAML.parse(readFileAndSubEnv(arg)), customizer)
} else {
console.error(`Invalid file extension: ${arg}`)
printHelp()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"fast-json-patch": "2.0.6",
"glob": "7.1.2",
"lodash.merge": "4.6.2",
"lodash.mergewith": "4.6.2",
"toml-j0.4": "1.1.1",
"tomlify-j0.4": "3.0.0",
"yaml": "1.10.0"
Expand Down
16 changes: 15 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ docker run --rm \
-f toml "*"
echo ""

echo "YAML:"
echo "YAML array merge:"
docker run --rm \
-v "$(pwd)/test/mix/:/home/node/" \
boxboat/config-merge \
"*"
echo ""

echo "YAML array overwrite:"
docker run --rm \
-v "$(pwd)/test/mix/:/home/node/" \
boxboat/config-merge \
-a overwrite "*"
echo ""

echo "YAML array concat:"
docker run --rm \
-v "$(pwd)/test/mix/:/home/node/" \
boxboat/config-merge \
-a concat "*"
echo ""

echo "Docker Compose"
docker run --rm \
-v "$(pwd)/test/docker-compose/:/home/node/" \
Expand Down
4 changes: 3 additions & 1 deletion test/mix/10.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
override-yaml: override
override-yaml: override
array-obj:
- key3: key3
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ inherits@2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

lodash.merge@4.6.2:
lodash.mergewith@4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==

minimatch@^3.0.4:
version "3.0.4"
Expand Down Expand Up @@ -85,7 +86,7 @@ wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

yaml@^1.10.0:
[email protected]:
version "1.10.0"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
integrity sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==

0 comments on commit ca18253

Please sign in to comment.