Skip to content

Instantly share code, notes, and snippets.

@bermi
Created March 9, 2020 18:35
Show Gist options
  • Save bermi/14f6584215dd1524754077b839c1a775 to your computer and use it in GitHub Desktop.
Save bermi/14f6584215dd1524754077b839c1a775 to your computer and use it in GitHub Desktop.
Exposes key value pairs on a yaml file as environment variables
#!/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