Change builtin templates to resemble jinja

This commit is contained in:
Tim Byrne 2019-10-01 08:17:28 -05:00
parent 234055190b
commit e999929818
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
2 changed files with 56 additions and 53 deletions

View File

@ -8,56 +8,56 @@ LOCAL_USER = "builtin_Test+@-!^User"
LOCAL_DISTRO = "builtin_Test+@-!^Distro" LOCAL_DISTRO = "builtin_Test+@-!^Distro"
TEMPLATE = f''' TEMPLATE = f'''
start of template start of template
builtin class = >YADM_CLASS< builtin class = >{{{{yadm.class}}}}<
builtin os = >YADM_OS< 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}}}}<
YADM_IF CLASS="wrongclass1" {{% if yadm.class == "wrongclass1" %}}
wrong class 1 wrong class 1
YADM_END {{% endif %}}
YADM_IF 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)
YADM_END {{% endif %}}
YADM_IF CLASS="wrongclass2" {{% if yadm.class == "wrongclass2" %}}
wrong class 2 wrong class 2
YADM_END {{% endif %}}
YADM_IF OS="wrongos1" {{% if yadm.os == "wrongos1" %}}
wrong os 1 wrong os 1
YADM_END {{% endif %}}
YADM_IF OS="{LOCAL_SYSTEM}" {{% if yadm.os == "{LOCAL_SYSTEM}" %}}
Included section for os = YADM_OS (YADM_OS repeated) Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
YADM_END {{% endif %}}
YADM_IF OS="wrongos2" {{% if yadm.os == "wrongos2" %}}
wrong os 2 wrong os 2
YADM_END {{% endif %}}
YADM_IF HOSTNAME="wronghost1" {{% if yadm.hostname == "wronghost1" %}}
wrong host 1 wrong host 1
YADM_END {{% endif %}}
YADM_IF HOSTNAME="{LOCAL_HOST}" {{% if yadm.hostname == "{LOCAL_HOST}" %}}
Included section for host = YADM_HOSTNAME (YADM_HOSTNAME repeated) Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
YADM_END {{% endif %}}
YADM_IF HOSTNAME="wronghost2" {{% if yadm.hostname == "wronghost2" %}}
wrong host 2 wrong host 2
YADM_END {{% endif %}}
YADM_IF USER="wronguser1" {{% if yadm.user == "wronguser1" %}}
wrong user 1 wrong user 1
YADM_END {{% endif %}}
YADM_IF USER="{LOCAL_USER}" {{% if yadm.user == "{LOCAL_USER}" %}}
Included section for user = YADM_USER (YADM_USER repeated) Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated)
YADM_END {{% endif %}}
YADM_IF USER="wronguser2" {{% if yadm.user == "wronguser2" %}}
wrong user 2 wrong user 2
YADM_END {{% endif %}}
YADM_IF DISTRO="wrongdistro1" {{% if yadm.distro == "wrongdistro1" %}}
wrong distro 1 wrong distro 1
YADM_END {{% endif %}}
YADM_IF DISTRO="{LOCAL_DISTRO}" {{% if yadm.distro == "{LOCAL_DISTRO}" %}}
Included section for distro = YADM_DISTRO (YADM_DISTRO repeated) Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
YADM_END {{% endif %}}
YADM_IF DISTRO="wrongdistro2" {{% if yadm.distro == "wrongdistro2" %}}
wrong distro 2 wrong distro 2
YADM_END {{% endif %}}
end of template end of template
''' '''
EXPECTED = f''' EXPECTED = f'''
@ -69,9 +69,9 @@ builtin user = >{LOCAL_USER}<
builtin distro = >{LOCAL_DISTRO}< builtin distro = >{LOCAL_DISTRO}<
Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated) Included section for class = {LOCAL_CLASS} ({LOCAL_CLASS} repeated)
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} repeated) 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)
Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} repeated) Included section for distro = {LOCAL_DISTRO} ({LOCAL_DISTRO} again)
end of template end of template
''' '''

25
yadm
View File

@ -279,35 +279,38 @@ function template_builtin() {
input="$1" input="$1"
output="$2" output="$2"
# the explicit "space + tab" character class used below is used because not
# all versions of awk seem to support the POSIX character classes [[:blank:]]
awk_pgm=$(cat << "EOF" awk_pgm=$(cat << "EOF"
# built-in template processor # built-in template processor
BEGIN { BEGIN {
c["CLASS"] = class blank = "[ ]"
c["OS"] = os c["class"] = class
c["HOSTNAME"] = host c["os"] = os
c["USER"] = user c["hostname"] = host
c["DISTRO"] = distro c["user"] = user
c["distro"] = distro
valid = conditions() valid = conditions()
end = "^YADM_END$" end = "^{%" blank "*endif" blank "*%}$"
skip = "^YADM_(IF|END$)" skip = "^{%" blank "*(if|endif)"
} }
{ replace_vars() } # variable replacements { replace_vars() } # variable replacements
$0 ~ valid, $0 ~ end { if ($0 ~ skip) next } # valid conditional blocks $0 ~ valid, $0 ~ end { if ($0 ~ skip) next } # valid conditional blocks
/^YADM_IF/, $0 ~ end { next } # invalid conditional blocks $0 ~ ("^{%" blank "*if"), $0 ~ end { next } # invalid conditional blocks
{ print } { print }
function replace_vars() { function replace_vars() {
for (label in c) { for (label in c) {
gsub(("YADM_" label), c[label]) gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
} }
} }
function conditions() { function conditions() {
pattern = "^(" pattern = "^{%" blank "*if" blank "*("
for (label in c) { for (label in c) {
value = c[label] value = c[label]
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value) gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
pattern = sprintf("%sYADM_IF +%s *= *\"%s\"|", pattern, label, value) pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value)
} }
sub(/\|$/,")",pattern) sub(/\|$/,")",pattern)
return pattern return pattern