From 444622a658794a9090d9c8b792894a03b987c1b6 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sat, 5 Oct 2019 11:01:13 -0500 Subject: [PATCH] Support `else` statements in builtin templates --- test/test_unit_template_builtin.py | 10 ++++++++++ yadm | 23 ++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/test/test_unit_template_builtin.py b/test/test_unit_template_builtin.py index 4d87a12..a699f6a 100644 --- a/test/test_unit_template_builtin.py +++ b/test/test_unit_template_builtin.py @@ -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) diff --git a/yadm b/yadm index 2a9e989..e4df305 100755 --- a/yadm +++ b/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) {