Last active
June 2, 2021 17:21
-
-
Save TomKeur/b83925487183b425fb83558b07279607 to your computer and use it in GitHub Desktop.
DotEnv to JSON (useful for for HashiCorp Vault)
This file contains 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 python | |
import json | |
import sys | |
try: | |
dotenv = sys.argv[1] | |
except IndexError as e: | |
dotenv = 'symfony-flex-environment.env' | |
with open(dotenv, 'r') as f: | |
content = f.readlines() | |
# Remove whitespace chars like "\n" at the end of each line | |
content = dict([x.strip().split('=', 1) for x in content if '=' in x]) | |
# Remove items starting with a # and replace ' with nothing. | |
newdict = {} | |
for key, value in content.items(): | |
if not key.startswith('#'): | |
newdict[key] = value.replace("'", '') | |
print(json.dumps(dict(newdict), indent=4, sort_keys=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment