Support else
statements in builtin templates
This commit is contained in:
parent
81134c8edb
commit
444622a658
2 changed files with 26 additions and 7 deletions
|
@ -13,11 +13,19 @@ builtin os = >{{{{yadm.os}}}}<
|
|||
builtin host = >{{{{yadm.hostname}}}}<
|
||||
builtin user = >{{{{yadm.user}}}}<
|
||||
builtin distro = >{{{{yadm.distro}}}}<
|
||||
{{% if yadm.class == "else1" %}}
|
||||
wrong else 1
|
||||
{{% else %}}
|
||||
Included section from else
|
||||
{{% endif %}}
|
||||
{{% if yadm.class == "wrongclass1" %}}
|
||||
wrong class 1
|
||||
{{% endif %}}
|
||||
{{% if yadm.class == "{LOCAL_CLASS}" %}}
|
||||
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
|
||||
Multiple lines
|
||||
{{% else %}}
|
||||
Should not be included...
|
||||
{{% endif %}}
|
||||
{{% if yadm.class == "wrongclass2" %}}
|
||||
wrong class 2
|
||||
|
@ -67,7 +75,9 @@ builtin os = >{LOCAL_SYSTEM}<
|
|||
builtin host = >{LOCAL_HOST}<
|
||||
builtin user = >{LOCAL_USER}<
|
||||
builtin distro = >{LOCAL_DISTRO}<
|
||||
Included section from else
|
||||
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
|
||||
Multiple lines
|
||||
Included section for os = {LOCAL_SYSTEM} ({LOCAL_SYSTEM} repeated)
|
||||
Included section for host = {LOCAL_HOST} ({LOCAL_HOST} again)
|
||||
Included section for user = {LOCAL_USER} ({LOCAL_USER} repeated)
|
||||
|
|
23
yadm
23
yadm
|
@ -290,21 +290,30 @@ BEGIN {
|
|||
c["hostname"] = host
|
||||
c["user"] = user
|
||||
c["distro"] = distro
|
||||
valid = conditions()
|
||||
vld = conditions()
|
||||
ifs = "^{%" blank "*if"
|
||||
els = "^{%" blank "*else" blank "*%}$"
|
||||
end = "^{%" blank "*endif" blank "*%}$"
|
||||
skip = "^{%" blank "*(if|endif)"
|
||||
skp = "^{%" blank "*(if|else|endif)"
|
||||
prt = 1
|
||||
}
|
||||
{ replace_vars() } # variable replacements
|
||||
$0 ~ valid, $0 ~ end { if ($0 ~ skip) next } # valid conditional blocks
|
||||
$0 ~ ("^{%" blank "*if"), $0 ~ end { next } # invalid conditional blocks
|
||||
{ print }
|
||||
|
||||
$0 ~ vld, $0 ~ end {
|
||||
if ($0 ~ vld || $0 ~ end) prt=1;
|
||||
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() {
|
||||
for (label in c) {
|
||||
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
|
||||
}
|
||||
}
|
||||
|
||||
function conditions() {
|
||||
pattern = "^{%" blank "*if" blank "*("
|
||||
for (label in c) {
|
||||
|
|
Loading…
Reference in a new issue