feat(parse_yaml): bump to latest commit
This commit is contained in:
parent
2394c2c430
commit
bd8628c97d
1 changed files with 135 additions and 97 deletions
|
@ -2,64 +2,96 @@
|
||||||
#
|
#
|
||||||
# This script may have been modified to work better with Blueprint.
|
# This script may have been modified to work better with Blueprint.
|
||||||
# Source code: https://github.com/mrbaseman/parse_yaml
|
# Source code: https://github.com/mrbaseman/parse_yaml
|
||||||
|
# License: https://raw.githubusercontent.com/mrbaseman/parse_yaml/master/LICENSE
|
||||||
|
|
||||||
function parse_yaml {
|
function parse_yaml {
|
||||||
local prefix=$2
|
unset i
|
||||||
local separator=${3:-_}
|
unset fs
|
||||||
|
local prefix=$2
|
||||||
|
local separator=${3:-_}
|
||||||
|
|
||||||
local indexfix
|
local indexfix=-1
|
||||||
# Detect awk flavor
|
# Detect awk flavor
|
||||||
if awk --version 2>&1 | grep -q "GNU Awk" ; then
|
if awk --version 2>&1 | grep -q "GNU Awk" ; then
|
||||||
# GNU Awk detected
|
# GNU Awk detected
|
||||||
indexfix=-1
|
indexfix=-1
|
||||||
elif awk -Wv 2>&1 | grep -q "mawk" ; then
|
elif awk -Wv 2>&1 | grep -q "mawk" ; then
|
||||||
# mawk detected
|
# mawk detected
|
||||||
indexfix=0
|
indexfix=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local s='[[:space:]]*' sm='[ \t]*' w='[a-zA-Z0-9_]*' fs=${fs:-$(echo @|tr @ '\034')} i=${i:- }
|
local s='[[:space:]]*' sm='[ \t]*' w='[a-zA-Z0-9_]*' fs=${fs:-$(echo @|tr @ '\034')} i=${i:- }
|
||||||
cat $1 | \
|
|
||||||
awk -F$fs "{multi=0;
|
###############################################################################
|
||||||
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
# cat: read the yaml file into the stream
|
||||||
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
# awk 1: process multi-line text
|
||||||
while(multi>0){
|
# sed 1: remove comments and empty lines
|
||||||
str=\$0; gsub(/^$sm/,\"\", str);
|
# sed 2: process lists
|
||||||
indent=index(\$0,str);
|
# sed 3: process dictionaries
|
||||||
indentstr=substr(\$0, 0, indent+$indexfix) \"$i\";
|
# sed 4: rearrange anchors
|
||||||
obuf=\$0;
|
# sed 5: remove '---'/'...'/quotes, add file separator to create fields for awk 2
|
||||||
getline;
|
# awk 2: convert the formatted data to variable assignments
|
||||||
while(index(\$0,indentstr)){
|
###############################################################################
|
||||||
obuf=obuf substr(\$0, length(indentstr)+1);
|
|
||||||
if (multi==1){obuf=obuf \"\\\\n\";}
|
cat $1 | \
|
||||||
if (multi==2){
|
awk -F$fs "{multi=0;
|
||||||
if(match(\$0,/^$sm$/))
|
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
||||||
obuf=obuf \"\\\\n\";
|
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
||||||
else obuf=obuf \" \";
|
while(multi>0){
|
||||||
}
|
str=\$0; gsub(/^$sm/,\"\", str);
|
||||||
getline;
|
indent=index(\$0,str);
|
||||||
}
|
indentstr=substr(\$0, 0, indent+$indexfix) \"$i\";
|
||||||
sub(/$sm$/,\"\",obuf);
|
obuf=\$0;
|
||||||
print obuf;
|
getline;
|
||||||
multi=0;
|
while(index(\$0,indentstr)){
|
||||||
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
obuf=obuf substr(\$0, length(indentstr)+1);
|
||||||
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
if (multi==1){obuf=obuf \"\\\\n\";}
|
||||||
}
|
if (multi==2){
|
||||||
print}" | \
|
if(match(\$0,/^$sm$/))
|
||||||
sed -e "s|^\($s\)?|\1-|" \
|
obuf=obuf \"\\\\n\";
|
||||||
-ne "s|^$s#.*||;s|$s#[^\"']*$||;s|^\([^\"'#]*\)#.*|\1|;t1;t;:1;s|^$s\$||;t2;p;:2;d" | \
|
else obuf=obuf \" \";
|
||||||
sed -ne "s|,$s\]$s\$|]|" \
|
}
|
||||||
-e ":1;s|^\($s\)\($w\)$s:$s\(&$w\)\?$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1\2: \3[\4]\n\1$i- \5|;t1" \
|
getline;
|
||||||
-e "s|^\($s\)\($w\)$s:$s\(&$w\)\?$s\[$s\(.*\)$s\]|\1\2: \3\n\1$i- \4|;" \
|
}
|
||||||
-e ":2;s|^\($s\)-$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1- [\2]\n\1$i- \3|;t2" \
|
sub(/$sm$/,\"\",obuf);
|
||||||
|
print obuf;
|
||||||
|
multi=0;
|
||||||
|
if(match(\$0,/$sm\|$sm$/)){multi=1; sub(/$sm\|$sm$/,\"\");}
|
||||||
|
if(match(\$0,/$sm>$sm$/)){multi=2; sub(/$sm>$sm$/,\"\");}
|
||||||
|
}
|
||||||
|
print}" | \
|
||||||
|
sed -e "s|^\($s\)?|\1-|" \
|
||||||
|
-ne "s|^\($s\)-$s\($w\)$s:$s\(.*\)|\1-\n\1 \2: \3|" \
|
||||||
|
-ne "s|^$s#.*||;s|$s#[^\"']*$||;s|^\([^\"'#]*\)#.*|\1|;t 1" \
|
||||||
|
-ne "t" \
|
||||||
|
-ne ":1" \
|
||||||
|
-ne "s|^$s\$||;t 2" \
|
||||||
|
-ne "p" \
|
||||||
|
-ne ":2" \
|
||||||
|
-ne "d" | \
|
||||||
|
sed -ne "s|,$s\]|]|g" \
|
||||||
|
-e ":1" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s\(&$w\)$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1\2: \3[\4]\n\1$i- \5|;t 1" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s\(&$w\)$s\[$s\(.*\)$s\]|\1\2: \3\n\1$i- \4|;" \
|
||||||
|
-e ":2" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1\2: [\3]\n\1$i- \4|;t 2" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s\[$s\(.*\)$s\]|\1\2:\n\1$i- \3|;" \
|
||||||
|
-e ":3" \
|
||||||
|
-e "s|^\($s\)-$s\[$s\(.*\)$s,$s\(.*\)$s\]|\1- [\2]\n\1$i- \3|;t 3" \
|
||||||
-e "s|^\($s\)-$s\[$s\(.*\)$s\]|\1-\n\1$i- \2|;p" | \
|
-e "s|^\($s\)-$s\[$s\(.*\)$s\]|\1-\n\1$i- \2|;p" | \
|
||||||
sed -ne "s|,$s}$s\$|}|" \
|
sed -ne "s|,$s}|}|g" \
|
||||||
-e ":1;s|^\($s\)-$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1- {\2}\n\1$i\3: \4|;t1" \
|
-e ":1" \
|
||||||
|
-e "s|^\($s\)-$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1- {\2}\n\1$i\3: \4|;t 1" \
|
||||||
-e "s|^\($s\)-$s{$s\(.*\)$s}|\1-\n\1$i\2|;" \
|
-e "s|^\($s\)-$s{$s\(.*\)$s}|\1-\n\1$i\2|;" \
|
||||||
-e ":2;s|^\($s\)\($w\)$s:$s\(&$w\)\?$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1\2: \3 {\4}\n\1$i\5: \6|;t2" \
|
-e ":2" \
|
||||||
-e "s|^\($s\)\($w\)$s:$s\(&$w\)\?$s{$s\(.*\)$s}|\1\2: \3\n\1$i\4|;p" | \
|
-e "s|^\($s\)\($w\)$s:$s\(&$w\)$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1\2: \3 {\4}\n\1$i\5: \6|;t 2" \
|
||||||
sed -e "s|^\($s\)\($w\)$s:$s\(&$w\)\(.*\)|\1\2:\4\n\3|" \
|
-e "s|^\($s\)\($w\)$s:$s\(&$w\)$s{$s\(.*\)$s}|\1\2: \3\n\1$i\4|;" \
|
||||||
|
-e ":3" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s{$s\(.*\)$s,$s\($w\)$s:$s\(.*\)$s}|\1\2: {\3}\n\1$i\4: \5|;t 3" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s{$s\(.*\)$s}|\1\2:\n\1$i\3|;p" | \
|
||||||
|
sed -e "s|^\($s\)\($w\)$s:$s\(&$w\)\(.*\)|\1\2:\4\n\3|" \
|
||||||
-e "s|^\($s\)-$s\(&$w\)\(.*\)|\1- \3\n\2|" | \
|
-e "s|^\($s\)-$s\(&$w\)\(.*\)|\1- \3\n\2|" | \
|
||||||
sed -ne "s|^\($s\):|\1|" \
|
sed -ne "s|^\($s\):|\1|" \
|
||||||
-e "s|^\($s\)\(---\)\($s\)||" \
|
-e "s|^\($s\)\(---\)\($s\)||" \
|
||||||
-e "s|^\($s\)\(\.\.\.\)\($s\)||" \
|
-e "s|^\($s\)\(\.\.\.\)\($s\)||" \
|
||||||
-e "s|^\($s\)-$s[\"']\(.*\)[\"']$s\$|\1$fs$fs\2|p;t" \
|
-e "s|^\($s\)-$s[\"']\(.*\)[\"']$s\$|\1$fs$fs\2|p;t" \
|
||||||
|
@ -68,50 +100,56 @@ function parse_yaml {
|
||||||
-e "s|^\($s\)\($w\)$s:$s[\"']\?\(.*\)$s\$|\1$fs\2$fs\3|" \
|
-e "s|^\($s\)\($w\)$s:$s[\"']\?\(.*\)$s\$|\1$fs\2$fs\3|" \
|
||||||
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)[\"']$s\$|\1$fs$fs$fs\2|" \
|
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)[\"']$s\$|\1$fs$fs$fs\2|" \
|
||||||
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)$s\$|\1$fs$fs$fs\2|" \
|
-e "s|^\($s\)[\"']\?\([^&][^$fs]\+\)$s\$|\1$fs$fs$fs\2|" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)$s\$|\1$fs\2$fs\3|" \
|
||||||
|
-e "s|^\($s\)[\"']\([^&][^$fs]*\)[\"']$s\$|\1$fs$fs$fs\2|" \
|
||||||
|
-e "s|^\($s\)[\"']\([^&][^$fs]*\)$s\$|\1$fs$fs$fs\2|" \
|
||||||
|
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|" \
|
||||||
|
-e "s|^\($s\)\([^&][^$fs]*\)[\"']$s\$|\1$fs$fs$fs\2|" \
|
||||||
|
-e "s|^\($s\)\([^&][^$fs]*\)$s\$|\1$fs$fs$fs\2|" \
|
||||||
-e "s|$s\$||p" | \
|
-e "s|$s\$||p" | \
|
||||||
awk -F$fs "{
|
awk -F$fs "{
|
||||||
gsub(/\t/,\" \",\$1);
|
gsub(/\t/,\" \",\$1);
|
||||||
if(NF>3){if(value!=\"\"){value = value \" \";}value = value \$4;}
|
if(NF>3){if(value!=\"\"){value = value \" \";}value = value \$4;}
|
||||||
else {
|
else {
|
||||||
if(match(\$1,/^&/)){anchor[substr(\$1,2)]=full_vn;getline};
|
if(match(\$1,/^&/)){anchor[substr(\$1,2)]=full_vn;getline};
|
||||||
indent = length(\$1)/length(\"$i\");
|
indent = length(\$1)/length(\"$i\");
|
||||||
vname[indent] = \$2;
|
vname[indent] = \$2;
|
||||||
value= \$3;
|
value= \$3;
|
||||||
for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
|
for (i in vname) {if (i > indent) {delete vname[i]; idx[i]=0}}
|
||||||
if(length(\$2)== 0){ vname[indent]= ++idx[indent] };
|
if(length(\$2)== 0){ vname[indent]= ++idx[indent] };
|
||||||
vn=\"\"; for (i=0; i<indent; i++) { vn=(vn)(vname[i])(\"$separator\")}
|
vn=\"\"; for (i=0; i<indent; i++) { vn=(vn)(vname[i])(\"$separator\")}
|
||||||
vn=\"$prefix\" vn;
|
vn=\"$prefix\" vn;
|
||||||
full_vn=vn vname[indent];
|
full_vn=vn vname[indent];
|
||||||
if(vn==\"$prefix\")vn=\"$prefix$separator\";
|
if(vn==\"$prefix\")vn=\"$prefix$separator\";
|
||||||
if(vn==\"_\")vn=\"__\";
|
if(vn==\"_\")vn=\"__\";
|
||||||
}
|
}
|
||||||
assignment[full_vn]=value;
|
assignment[full_vn]=value;
|
||||||
if(!match(assignment[vn], full_vn))assignment[vn]=assignment[vn] \" \" full_vn;
|
if(!match(assignment[vn], full_vn))assignment[vn]=assignment[vn] \" \" full_vn;
|
||||||
if(match(value,/^\*/)){
|
if(match(value,/^\*/)){
|
||||||
ref=anchor[substr(value,2)];
|
ref=anchor[substr(value,2)];
|
||||||
if(length(ref)==0){
|
if(length(ref)==0){
|
||||||
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
||||||
} else {
|
} else {
|
||||||
for(val in assignment){
|
for(val in assignment){
|
||||||
if((length(ref)>0)&&index(val, ref)==1){
|
if((length(ref)>0)&&index(val, ref)==1){
|
||||||
tmpval=assignment[val];
|
tmpval=assignment[val];
|
||||||
sub(ref,full_vn,val);
|
sub(ref,full_vn,val);
|
||||||
if(match(val,\"$separator\$\")){
|
if(match(val,\"$separator\$\")){
|
||||||
gsub(ref,full_vn,tmpval);
|
gsub(ref,full_vn,tmpval);
|
||||||
} else if (length(tmpval) > 0) {
|
} else if (length(tmpval) > 0) {
|
||||||
printf(\"%s=\\\"%s\\\"\n\", val, tmpval);
|
printf(\"%s=\\\"%s\\\"\n\", val, tmpval);
|
||||||
}
|
}
|
||||||
assignment[val]=tmpval;
|
assignment[val]=tmpval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (length(value) > 0) {
|
} else if (length(value) > 0) {
|
||||||
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
printf(\"%s=\\\"%s\\\"\n\", full_vn, value);
|
||||||
}
|
}
|
||||||
}END{
|
}END{
|
||||||
for(val in assignment){
|
for(val in assignment){
|
||||||
if(match(val,\"$separator\$\"))
|
if(match(val,\"$separator\$\"))
|
||||||
printf(\"%s=\\\"%s\\\"\n\", val, assignment[val]);
|
printf(\"%s=\\\"%s\\\"\n\", val, assignment[val]);
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
}
|
}
|
Loading…
Reference in a new issue