Skip to content

Instantly share code, notes, and snippets.

@antonagestam
Last active February 24, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonagestam/e4889bc5059b1f1c39e53cc9612c2250 to your computer and use it in GitHub Desktop.
Save antonagestam/e4889bc5059b1f1c39e53cc9612c2250 to your computer and use it in GitHub Desktop.
Make bash export statements from docker env file
"""
$ python3 export.py <(echo -e '# this is a comment\nKEY=value123')
export KEY='value123'
$ (. <(python3 export.py <(echo 'KEY=value123')) && echo $KEY)
value123
"""
import re
import fileinput
pattern = re.compile("^(?P<key>[A-z_]+)=(?P<value>.+)$", re.M)
for line in fileinput.input():
if match := pattern.fullmatch(line.strip()):
print(f"export {match.group('key')}='{match.group('value')}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment