Skip to content

Instantly share code, notes, and snippets.

@Ashex
Last active July 20, 2016 18:39
Show Gist options
  • Save Ashex/862e9a200d58cf39a3418f7bf765e1eb to your computer and use it in GitHub Desktop.
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.
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