Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bcopy/a12c8b5eb01fc606beac4463128a04fd to your computer and use it in GitHub Desktop.
Save bcopy/a12c8b5eb01fc606beac4463128a04fd to your computer and use it in GitHub Desktop.

How to use

Install dependencies

sudo easy_install everyconfig visitor

or

sudo pip install everyconfig visitor

get the sample file

wget https://gist.githubusercontent.com/bcopy/a12c8b5eb01fc606beac4463128a04fd/raw/a7d664a35d17826e6c419ee422505d0872bdb0cb/default.yaml

chmod and Run !

chmod 755 yaml_to_env.py
python yaml_to_env.py .
EDITOR: /usr/bin/nano
PREFIX: /usr/local
CC: gcc
# Sass
SASS_PATH: [~/.sasslib]
# Node.js load paths
NODE_PATH: # using an alternate installation due to problem with Homebrew on Mavericks
- ~/.node/lib
- ~/.node/lib/node_modules
# Directories to add to the $LOAD_PATH variable for all Ruby interpreters
RUBYLIB:
- ~/.rubylib
#!/usr/bin/env python
##############################
# This script converts an everyconfig YAML fileset into a list of
# environment variables.
# Inspired by https://gist.github.com/SteveBenner/588fff3e54636f3d8297 without the Ruby
# Depends on everyconfig and visitor packages
#
import sys
from visitor import Visitor
from everyconfig import everyconfig
if len(sys.argv) < 2:
msg = """Usage : yaml_to_env.py <settings folder> [<command prefix="export ">]
Purpose : convert a given YAML file into a set of environment variables to produce a properties file or shell export directives.
"""
print msg
raise RuntimeError("Please provide an input YAML path")
varsYaml = everyconfig(sys.argv[1])
if len(sys.argv) > 2:
cmd_prefix = sys.argv[2]
else:
cmd_prefix = "export "
if varsYaml is None:
raise RuntimeError("Could not parse input YAML path")
class EnvEncoder(Visitor):
def __init__(self):
self.parentKey = ""
def visit_str(self, node):
print cmd_prefix+self.parentKey+"="+node
def visit_int(self, node):
return self.visit(str(node))
def visit_bool(self, node):
return self.visit(str(node))
def visit_list(self, node):
if len(node) > 0:
baseParentKey = self.parentKey+("" if self.parentKey =="" else "_")
print cmd_prefix+baseParentKey+"COUNT="+str(len(node))
for index, item in enumerate(node):
self.parentKey = baseParentKey+str(index)
self.visit(item)
def visit_dict(self, node):
prevParent = self.parentKey
for key, value in sorted(node.items()):
self.parentKey = prevParent+("" if prevParent =="" else "_")+key.upper()
if type(value) == str:
print cmd_prefix+self.parentKey+"="+value
else:
self.visit(value)
# Pop the last known key
self.parentKey = prevParent
EnvEncoder().visit(varsYaml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment