Support `else` statements in builtin templates

This commit is contained in:
Tim Byrne 2019-10-05 11:01:13 -05:00
parent 81134c8edb
commit 444622a658
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
2 changed files with 26 additions and 7 deletions

View File

@ -13,11 +13,19 @@ builtin os = >{{{{yadm.os}}}}<
builtin host = >{{{{yadm.hostname}}}}< builtin host = >{{{{yadm.hostname}}}}<
builtin user = >{{{{yadm.user}}}}< builtin user = >{{{{yadm.user}}}}<
builtin distro = >{{{{yadm.distro}}}}< builtin distro = >{{{{yadm.distro}}}}<
{{% if yadm.class == "else1" %}}
wrong else 1
{{% else %}}
Included section from else
{{% endif %}}
{{% if yadm.class == "wrongclass1" %}} {{% if yadm.class == "wrongclass1" %}}
wrong class 1 wrong class 1
{{% endif %}} {{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS}" %}} {{% if yadm.class == "{LOCAL_CLASS}" %}}
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated) Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
Multiple lines
{{% else %}}
Should not be included...
{{% endif %}} {{% endif %}}
{{% if yadm.class == "wrongclass2" %}} {{% if yadm.class == "wrongclass2" %}}
wrong class 2 wrong class 2
@ -67,7 +75,9 @@ builtin os = >{LOCAL_SYSTEM}<
builtin host = >{LOCAL_HOST}< builtin host = >{LOCAL_HOST}<
builtin user = >{LOCAL_USER}< builtin user = >{LOCAL_USER}<
builtin distro = >{LOCAL_DISTRO}< builtin distro = >{LOCAL_DISTRO}<
Included section from else
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated) Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
Multiple lines
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated) Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again) Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated) Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)

23
yadm
View File

@ -290,21 +290,30 @@ BEGIN {
c["hostname"] = host c["hostname"] = host
c["user"] = user c["user"] = user
c["distro"] = distro c["distro"] = distro
valid = conditions() vld = conditions()
ifs = "^{%" blank "*if"
els = "^{%" blank "*else" blank "*%}$"
end = "^{%" blank "*endif" blank "*%}$" end = "^{%" blank "*endif" blank "*%}$"
skip = "^{%" blank "*(if|endif)" skp = "^{%" blank "*(if|else|endif)"
prt = 1
} }
{ replace_vars() } # variable replacements { replace_vars() } # variable replacements
$0 ~ valid, $0 ~ end { if ($0 ~ skip) next } # valid conditional blocks $0 ~ vld, $0 ~ end {
$0 ~ ("^{%" blank "*if"), $0 ~ end { next } # invalid conditional blocks if ($0 ~ vld || $0 ~ end) prt=1;
{ print } if ($0 ~ els) prt=0;
if ($0 ~ skp) next;
}
($0 ~ ifs && $0 !~ vld), $0 ~ end {
if ($0 ~ ifs && $0 !~ vld) prt=0;
if ($0 ~ els || $0 ~ end) prt=1;
if ($0 ~ skp) next;
}
{ if (prt) print }
function replace_vars() { function replace_vars() {
for (label in c) { for (label in c) {
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label]) gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
} }
} }
function conditions() { function conditions() {
pattern = "^{%" blank "*if" blank "*(" pattern = "^{%" blank "*if" blank "*("
for (label in c) { for (label in c) {