Last active
July 20, 2016 18:39
-
-
Save Ashex/862e9a200d58cf39a3418f7bf765e1eb to your computer and use it in GitHub Desktop.
Bash function that will replace a value in a json file based off a variable matching a pattern. Useful for docker containers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
change_api_keys () | |
{ | |
# Change the API keys in credentials.json | |
# If a variable with the value is provided | |
for VAR in `env` | |
do | |
case "$VAR" in | |
POKEMON_* ) | |
key_name=`echo "$VAR" | sed -e "s/^POKEMON_\(.*\)\=.*/\1/"` | |
echo "Changing value of " $key_name | |
key_value=`echo "$VAR" | sed -e "s/.*=\(.*\)/\1/"` | |
cat credentials.json | | |
jq "to_entries | | |
map(if .key == \"$key_name\" | |
then . + {\"value\":\"$key_value\"} | |
else . | |
end | |
) | | |
from_entries" > modified_credentials.json | |
mv modified_credentials.json credentials.json | |
;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment