Created
March 9, 2020 18:35
-
-
Save bermi/14f6584215dd1524754077b839c1a775 to your computer and use it in GitHub Desktop.
Exposes key value pairs on a yaml file as environment variables
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
#!/usr/bin/env bash | |
# Exports a flat yaml key:value pairs as environment variables | |
# | |
# To source a yaml file as environment variables run | |
# | |
# . yaml-source env.yml | |
# | |
eval $( | |
cat $1 | | |
# limit only to lines that contain a valid KEY | |
grep -E '^[A-Za-z0-9_ ]+:' | | |
# replace `key:'values with quotes'` with `export key='value with quotes'` | |
sed -E 's/^([A-Za-z0-9_]+) *: *([\x27"])/export \1=\2/' | | |
# quote items that missed the quotes | |
sed -E 's/^([A-Za-z0-9_]+) *: *(.+)/export \1="\2"/' | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment