Last active
June 12, 2020 20:34
-
-
Save sveesible/0d44aa9ad16d56e415ee to your computer and use it in GitHub Desktop.
Shell script to parse specific field's values from a given yaml file and register them as shell variables with the same field name
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
#!/bin/bash | |
## Based on https://gist.github.com/pkuczynski/8665367 | |
## derived from: | |
## https://gist.github.com/epiloque/8cf512c6d64641bde388 | |
## https://gist.github.com/DinoChiesa/3e3c3866b51290f31243 | |
YAML_FILE=test.yaml | |
debug=1 | |
function parse_yaml_values() { | |
fields=('development_adapter' 'development_authentication_class_parameters_attributes' 'development_roles') | |
parse_yaml ${YAML_FILE} | |
echo "variable development_adapter: ${development_adapter[*]}" | |
echo "variable development_roles: ${development_roles[*]}" | |
echo "variable development_authentication_class_parameters_attributes: ${development_authentication_class_parameters_attributes[*]}" | |
} | |
# Based on https://gist.github.com/pkuczynski/8665367 | |
# | |
# Works for arrays of hashes, and some hashes with arrays | |
# Variable names will be underscore delimitted based on nested parent names | |
# Send in yaml file as first argument and create a global array named $fields | |
# with necessary yaml field names fully underscore delimitted to match nesting | |
# then this will register those variables into the shell's scope if they exist in Yaml | |
# $VERBOSE=1 will also print the full values | |
# Defaults to indent of 4 so d=4 | |
# To use with indent of 2 change d to 2 | |
function parse_yaml() { | |
local s | |
local w | |
local fs | |
local d | |
s='[[:space:]]*' | |
w='[a-zA-Z0-9_]*' | |
fs="$(echo @|tr @ '\034')" | |
d=4 | |
eval $( | |
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ | |
-e "s|^\($s\)-\?$s\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | | |
awk -F"$fs" -v names="${fields[*]}" ' | |
BEGIN { split(names,n," ") } | |
{ sc=length($1) % "'$d'"; | |
if ( sc == 0 ) { | |
indent = length($1)/"'$d'" | |
} else { | |
indent = (length($1)+("'$d'"-sc))/"'$d'" | |
} | |
vname[indent] = $2; | |
for (i in vname) {if (i > indent){ delete vname[i];}} | |
if (length($3) > 0 ) { | |
vn=""; | |
for (i=0; i<indent; i++) { | |
if (length(vname[i]) > 0) {vn=(vn)(vname[i])("_");} | |
} | |
ap=""; | |
if($2 ~ /^ *$/ && vn ~ /_$/) { vn=substr(vn,1,length(vn)-1);ap="+" } | |
for ( name in n ) { | |
if ( $2 == n[name] || vn == n[name] || (vn)($2) == n[name]) { | |
printf("%s%s%s=(\"%s\")\n", vn, $2, ap, $3); | |
if ("'"$debug"'" == "1"){ | |
printf(";logdebug %s%s%s=\\(\\\"%s\\\"\\);", vn, $2, ap, $3); | |
} | |
} | |
} | |
} | |
}' | |
) | |
} | |
parse_yaml_values |
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
development: | |
adapter: mysql2 | |
encoding: utf8 | |
database: my_database | |
username: root | |
authentication: | |
- class: withname | |
parameters: | |
- attributes : | |
- foo | |
- bar,baz | |
roles: | |
- admin | |
- developer | |
- guest | |
password: | |
users: | |
- | |
name: pineapple | |
role: admin | |
- | |
name: umbrella | |
role: developer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment