-
-
Save colegatron/a593674c4fb04c6042aca1bd76c3028f to your computer and use it in GitHub Desktop.
Convert configuration YAML file into properties file or shell environment variables
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/python3 | |
import os | |
import re | |
import yaml | |
import sys | |
if not os.getenv("CONFIG_FILE"): | |
print("Usage:") | |
print("$ CONFIG_FILE=the/config.yaml " + sys.argv[0] + " < exports to yaml by default") | |
print("$ CONFIG_FILE=the/config.yaml " + sys.argv[0] + " yaml") | |
print("$ CONFIG_FILE=the/config.yaml " + sys.argv[0] + " bash") | |
print("$ CONFIG_FILE=the/config.yaml " + sys.argv[0] + " bash CONFIG < Adds a leading 'CONFIG_' to all vars") | |
sys.exit(1) | |
data = yaml.load(open(os.getenv("CONFIG_FILE"),'r')) | |
FMT = sys.argv[1].lower() if len(sys.argv)>1 else "yaml" | |
STARTWITH = sys.argv[2].lower() if len(sys.argv)==3 else "" | |
# Display name and value with format depending on type | |
# Values list are joined with commas | |
# Values in shell are protected from expansion | |
# Shell: caution: remember to quote them in shell to preserve \n and * | |
# Force variables, in any case, to contain ony digits, a-z and underscores | |
def display_var(k, v): | |
format = "{},{}" | |
if FMT == "yaml": format = "{}: {}" | |
if FMT == "bash": format = "export {}=\"{}\"" | |
if type(v) is list: v = ",".join(v) | |
if FMT == "bash" and isinstance(v,str): | |
v = v.replace("$","\$") | |
k = re.sub("[^a-zA-Z0-9_]", "_", k) | |
print(format.format(k, v)) | |
def print_level(data,start_in="", start_with=""): | |
start_in = start_in if not start_with else start_with | |
for subkey in data.keys(): | |
if hasattr(data[subkey],'items'): | |
restart_in = start_in + "_" + subkey if start_in else subkey | |
print_level(data[subkey],restart_in) | |
else: | |
display_var((start_in[len(start_with):]+"_"+subkey).upper(), data[subkey]) | |
print_level(data,start_with=STARTWITH) |
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
I use yaml files for global environments configuration. | |
Each environment (dev, prod, stage) has its own identical | |
structured yaml file with the environment's values. | |
To use them into bash scripts or containers and docker-compose | |
files as environment variables I just wrote this little script | |
to generate the proper output. | |
CAUTION: It modifies yaml keys in order to make them usable. | |
This feature will force everybody use nice names | |
Example input: | |
cfg: | |
google: | |
ownership_verification: | |
stage.domain.com: | |
upload_file_name: googlec862b21dd0613842e.html | |
Example output (bash): | |
# Variable name modified, original: CFG_GOOGLE_OWNERSHIP_VERIFICATION_STAGE.DOMAIN.COM_UPLOAD_FILE_NAME | |
export CFG_GOOGLE_OWNERSHIP_VERIFICATION_STAGE_DOMAIN_COM_UPLOAD_FILE_NAME="googlec862b21dd0613842e.html" | |
Example output (yaml): | |
# Variable name modified, original: CFG_GOOGLE_OWNERSHIP_VERIFICATION_STAGE.DOMAIN.COM_UPLOAD_FILE_NAME | |
CFG_GOOGLE_OWNERSHIP_VERIFICATION_STAGE_DOMAIN_COM_UPLOAD_FILE_NAME: googlec862b21dd0613842e.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment