Created
January 31, 2024 20:41
-
-
Save mjsqu/89dcd3901ec7cae4ce69af2294ae47f0 to your computer and use it in GitHub Desktop.
Meltano: Env Config Toggle
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 | |
# Takes an input file and determines if it is a .env or a JSON config file | |
# outputs the opposite of the input file to stdout for redirection to an env/config file | |
# useful for moving from meltano to tap testing, vice-versa | |
# optional arg[2] is prefix, which will chop off an .env prefix or add a prefix if going the other way | |
import sys | |
import json | |
import re | |
infile = sys.argv[1] | |
prefix = '' | |
if len(sys.argv) > 2: | |
prefix = sys.argv[2] + "_" | |
with open(infile,'r') as f: | |
""" | |
Have a go at parsing the input file as JSON - this means it is a config file | |
convert it to a dotenv file | |
""" | |
try: | |
config = json.load(f) | |
for k,v in config.items(): | |
print(f"{k.upper()}='{json.dumps(v)}'") | |
except: | |
f.seek(0) | |
config = {} | |
for line in f: | |
line = line.strip() | |
if not(re.match(r'^\s*#',line)) and re.match(f'^{prefix}\w+=',line): | |
kv = line.split('=') | |
if len(kv) == 2: | |
value = kv[1].strip("'").strip('"') | |
try: | |
value = json.loads(value) | |
config[kv[0].replace(prefix,'').lower()] = value | |
except: | |
config[kv[0].replace(prefix,'').lower()] = value | |
print(json.dumps(config,indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment