Skip to content

Instantly share code, notes, and snippets.

@robskillington
Created November 18, 2017 02:24
Show Gist options
  • Save robskillington/5d5c30f7eef49aff7b5c48cb1bae7f55 to your computer and use it in GitHub Desktop.
Save robskillington/5d5c30f7eef49aff7b5c48cb1bae7f55 to your computer and use it in GitHub Desktop.
# originally courtesy of https://gist.github.com/pkuczynski/8665367
# note: only supports 2 space indented YAML.
# additions:
# - only set environment variable if not already set
# - uppercase parsed variables to avoid case sensitivity
parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("test \"$%s%s\" != \"\" || export %s%s=\"%s\"; ", toupper(vn), toupper($2), toupper(vn), toupper($2), $3);
}
}'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment