Skip to content

Instantly share code, notes, and snippets.

@datfinesoul
Last active March 16, 2021 13:38
Show Gist options
  • Save datfinesoul/b75f136041b5ced33ca29c875319b729 to your computer and use it in GitHub Desktop.
Save datfinesoul/b75f136041b5ced33ca29c875319b729 to your computer and use it in GitHub Desktop.
Merge a list of keys into a JSON map and clean up old entries
[*.bash]
indent_style = tab
indent_size = 2
[*.{txt,json}]
indent_style = space
indent_size = 2
{
"test-app1--main": [
"v2-i50-builder",
"v1-i50-builder",
"v1-i51-builder"
],
"test-app2--main": [
"v1-i51-builder"
],
"someapp--master": [
"v1-i50-builder"
],
"another--one": [
"v2-i50-builder"
]
}
#!/usr/bin/env bash
tail -n1000 *.json *.txt
jq \
--slurpfile manifest ./manifest.json \
--rawfile updates ./updates.txt \
--null-input \
--arg builder "v1-i51-builder" \
--from-file <(cat <<- DOC
# remove any entries that match $builder
(\$manifest[] | map_values(del(.. | select(. == \$builder)))) as \$current
# read the raw file lines into an array and strip empty lines
| (\$updates | split("\n") | map(select(.|length > 0))
# create a updates object using the current repo-branches and remaining items
| reduce .[] as \$item ({}; {(\$item): (\$current[\$item] + [\$builder])} + .)) as \$override
# do an object assign
| \$current + \$override
# remove empty entries
| del(.[] | select(. | length == 0))
DOC
)
test-app1--main
another--one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment