1
0
Fork 0
mirror of synced 2024-12-21 22:21:08 -05:00

Ignore case in alt and default template processor conditions

This aligns all conditions with distro and distro_family.

Suggestion from #456.
This commit is contained in:
Erik Flodin 2024-12-09 23:50:49 +01:00
parent 6c1970fb41
commit c092b7c099
No known key found for this signature in database
GPG key ID: 420A7C865EE3F85F
4 changed files with 41 additions and 43 deletions

View file

@ -54,37 +54,37 @@ def calculate_score(filename):
if label in CONDITION["default"]["labels"]: if label in CONDITION["default"]["labels"]:
score += 1000 score += 1000
elif label in CONDITION["arch"]["labels"]: elif label in CONDITION["arch"]["labels"]:
if value == "testarch": if value.lower() == "testarch":
score += 1000 + CONDITION["arch"]["modifier"] score += 1000 + CONDITION["arch"]["modifier"]
else: else:
score = 0 score = 0
break break
elif label in CONDITION["system"]["labels"]: elif label in CONDITION["system"]["labels"]:
if value == "testsystem": if value.lower() == "testsystem":
score += 1000 + CONDITION["system"]["modifier"] score += 1000 + CONDITION["system"]["modifier"]
else: else:
score = 0 score = 0
break break
elif label in CONDITION["distro"]["labels"]: elif label in CONDITION["distro"]["labels"]:
if value == "testdistro": if value.lower() == "testdistro":
score += 1000 + CONDITION["distro"]["modifier"] score += 1000 + CONDITION["distro"]["modifier"]
else: else:
score = 0 score = 0
break break
elif label in CONDITION["class"]["labels"]: elif label in CONDITION["class"]["labels"]:
if value == "testclass": if value.lower() == "testclass":
score += 1000 + CONDITION["class"]["modifier"] score += 1000 + CONDITION["class"]["modifier"]
else: else:
score = 0 score = 0
break break
elif label in CONDITION["hostname"]["labels"]: elif label in CONDITION["hostname"]["labels"]:
if value == "testhost": if value.lower() == "testhost":
score += 1000 + CONDITION["hostname"]["modifier"] score += 1000 + CONDITION["hostname"]["modifier"]
else: else:
score = 0 score = 0
break break
elif label in CONDITION["user"]["labels"]: elif label in CONDITION["user"]["labels"]:
if value == "testuser": if value.lower() == "testuser":
score += 1000 + CONDITION["user"]["modifier"] score += 1000 + CONDITION["user"]["modifier"]
else: else:
score = 0 score = 0
@ -105,12 +105,12 @@ def calculate_score(filename):
def test_score_values(runner, yadm, default, arch, system, distro, cla, host, user): def test_score_values(runner, yadm, default, arch, system, distro, cla, host, user):
"""Test score results""" """Test score results"""
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
local_class = "testclass" local_class = "testClass"
local_arch = "testarch" local_arch = "testARch"
local_system = "testsystem" local_system = "TESTsystem"
local_distro = "testdistro" local_distro = "testDISTro"
local_host = "testhost" local_host = "testHost"
local_user = "testuser" local_user = "testUser"
filenames = {"filename##": 0} filenames = {"filename##": 0}
if default: if default:

View file

@ -35,13 +35,13 @@ wrong class 1
{{% if yadm.class != "wronglcass" %}} {{% if yadm.class != "wronglcass" %}}
Included section from != Included section from !=
{{% endif\t\t %}} {{% endif\t\t %}}
{{% if yadm.class == "{LOCAL_CLASS}" %}} {{% if yadm.class == "{LOCAL_CLASS.lower()}" %}}
Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated) Included section for class = {{{{yadm.class}}}} ({{{{yadm.class}}}} repeated)
Multiple lines Multiple lines
{{% else %}} {{% else %}}
Should not be included... Should not be included...
{{% endif %}} {{% endif %}}
{{% if yadm.class == "{LOCAL_CLASS2}" %}} {{% if yadm.class == "{LOCAL_CLASS2.upper()}" %}}
Included section for second class Included section for second class
{{% endif %}} {{% endif %}}
{{% if yadm.class == "wrongclass2" %}} {{% if yadm.class == "wrongclass2" %}}
@ -50,7 +50,7 @@ wrong class 2
{{% if yadm.arch == "wrongarch1" %}} {{% if yadm.arch == "wrongarch1" %}}
wrong arch 1 wrong arch 1
{{% endif %}} {{% endif %}}
{{% if yadm.arch == "{LOCAL_ARCH}" %}} {{% if yadm.arch == "{LOCAL_ARCH.title()}" %}}
Included section for arch = {{{{yadm.arch}}}} ({{{{yadm.arch}}}} repeated) Included section for arch = {{{{yadm.arch}}}} ({{{{yadm.arch}}}} repeated)
{{% endif %}} {{% endif %}}
{{% if yadm.arch == "wrongarch2" %}} {{% if yadm.arch == "wrongarch2" %}}
@ -59,7 +59,7 @@ wrong arch 2
{{% if yadm.os == "wrongos1" %}} {{% if yadm.os == "wrongos1" %}}
wrong os 1 wrong os 1
{{% endif %}} {{% endif %}}
{{% if yadm.os == "{LOCAL_SYSTEM}" %}} {{% if yadm.os == "{LOCAL_SYSTEM.lower()}" %}}
Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated) Included section for os = {{{{yadm.os}}}} ({{{{yadm.os}}}} repeated)
{{% endif %}} {{% endif %}}
{{% if yadm.os == "wrongos2" %}} {{% if yadm.os == "wrongos2" %}}
@ -68,7 +68,7 @@ wrong os 2
{{% if yadm.hostname == "wronghost1" %}} {{% if yadm.hostname == "wronghost1" %}}
wrong host 1 wrong host 1
{{% endif %}} {{% endif %}}
{{% if yadm.hostname == "{LOCAL_HOST}" %}} {{% if yadm.hostname == "{LOCAL_HOST.upper()}" %}}
Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again) Included section for host = {{{{yadm.hostname}}}} ({{{{yadm.hostname}}}} again)
{{% endif %}} {{% endif %}}
{{% if yadm.hostname == "wronghost2" %}} {{% if yadm.hostname == "wronghost2" %}}
@ -77,7 +77,7 @@ wrong host 2
{{% if yadm.user == "wronguser1" %}} {{% if yadm.user == "wronguser1" %}}
wrong user 1 wrong user 1
{{% endif %}} {{% endif %}}
{{% if yadm.user == "{LOCAL_USER}" %}} {{% if yadm.user == "{LOCAL_USER.title()}" %}}
Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated) Included section for user = {{{{yadm.user}}}} ({{{{yadm.user}}}} repeated)
{{% endif %}} {{% endif %}}
{{% if yadm.user == "wronguser2" %}} {{% if yadm.user == "wronguser2" %}}
@ -86,7 +86,7 @@ wrong user 2
{{% if yadm.distro == "wrongdistro1" %}} {{% if yadm.distro == "wrongdistro1" %}}
wrong distro 1 wrong distro 1
{{% endif %}} {{% endif %}}
{{% if yadm.distro == "{LOCAL_DISTRO.upper()}" %}} {{% if yadm.distro == "{LOCAL_DISTRO.lower()}" %}}
Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again) Included section for distro = {{{{yadm.distro}}}} ({{{{yadm.distro}}}} again)
{{% endif %}} {{% endif %}}
{{% if yadm.distro == "wrongdistro2" %}} {{% if yadm.distro == "wrongdistro2" %}}
@ -102,7 +102,7 @@ Included section for distro_family = \
{{% if yadm.distro_family == "wrongfamily2" %}} {{% if yadm.distro_family == "wrongfamily2" %}}
wrong family 2 wrong family 2
{{% endif %}} {{% endif %}}
{{% if env.VAR == "{ENV_VAR}" %}} {{% if env.VAR == "{ENV_VAR.title()}" %}}
Included section for env.VAR = {{{{env.VAR}}}} ({{{{env.VAR}}}} again) Included section for env.VAR = {{{{env.VAR}}}} ({{{{env.VAR}}}} again)
{{% endif %}} {{% endif %}}
{{% if env.VAR == "wrongenvvar" %}} {{% if env.VAR == "wrongenvvar" %}}

34
yadm
View file

@ -179,35 +179,32 @@ function score_file() {
local value=${field#*.} local value=${field#*.}
[ "$field" = "$label" ] && value="" # when .value is omitted [ "$field" = "$label" ] && value="" # when .value is omitted
shopt -s nocasematch
local -i delta=-1 local -i delta=-1
case "$label" in case "$label" in
default) default)
delta=0 delta=0
;; ;;
a|arch) a|arch)
[ "$value" = "$local_arch" ] && delta=1 [[ "$value" = "$local_arch" ]] && delta=1
;; ;;
o|os) o|os)
[ "$value" = "$local_system" ] && delta=2 [[ "$value" = "$local_system" ]] && delta=2
;; ;;
d|distro) d|distro)
shopt -s nocasematch
[[ "${value// /_}" = "${local_distro// /_}" ]] && delta=4 [[ "${value// /_}" = "${local_distro// /_}" ]] && delta=4
shopt -u nocasematch
;; ;;
f|distro_family) f|distro_family)
shopt -s nocasematch
[[ "${value// /_}" = "${local_distro_family// /_}" ]] && delta=8 [[ "${value// /_}" = "${local_distro_family// /_}" ]] && delta=8
shopt -u nocasematch
;; ;;
c|class) c|class)
in_list "$value" "${local_classes[@]}" && delta=16 in_list "$value" "${local_classes[@]}" && delta=16
;; ;;
h|hostname) h|hostname)
[ "$value" = "$local_host" ] && delta=32 [[ "$value" = "$local_host" ]] && delta=32
;; ;;
u|user) u|user)
[ "$value" = "$local_user" ] && delta=64 [[ "$value" = "$local_user" ]] && delta=64
;; ;;
e|extension) e|extension)
# extension isn't a condition and doesn't affect the score # extension isn't a condition and doesn't affect the score
@ -230,6 +227,7 @@ function score_file() {
INVALID_ALT+=("$source") INVALID_ALT+=("$source")
;; ;;
esac esac
shopt -u nocasematch
if (( delta < 0 )); then if (( delta < 0 )); then
score=0 score=0
@ -295,16 +293,14 @@ function record_score() {
function choose_template_cmd() { function choose_template_cmd() {
local kind="$1" local kind="$1"
if [ "$kind" = "default" ] || [ "$kind" = "" ]; then if [[ "${kind:-default}" = "default" ]]; then
awk_available && echo "template_default" awk_available && echo "template_default"
elif [ "$kind" = "esh" ]; then elif [[ "$kind" = "esh" ]]; then
esh_available && echo "template_esh" esh_available && echo "template_esh"
elif [ "$kind" = "j2cli" ] || [ "$kind" = "j2" ] && j2cli_available; then elif [[ "$kind" = "j2cli" || "$kind" = "j2" ]] && j2cli_available; then
echo "template_j2cli" echo "template_j2cli"
elif [ "$kind" = "envtpl" ] || [ "$kind" = "j2" ] && envtpl_available; then elif [[ "$kind" = "envtpl" || "$kind" = "j2" ]] && envtpl_available; then
echo "template_envtpl" echo "template_envtpl"
else
return # this "kind" of template is not supported
fi fi
} }
@ -354,21 +350,17 @@ BEGIN {
match($0, /[!=]=/) match($0, /[!=]=/)
op = substr($0, RSTART, RLENGTH) op = substr($0, RSTART, RLENGTH)
match($0, /".*"/) match($0, /".*"/)
rhs = replace_vars(substr($0, RSTART + 1, RLENGTH - 2)) rhs = tolower(replace_vars(substr($0, RSTART + 1, RLENGTH - 2)))
if (lhs == "yadm.class") { if (lhs == "yadm.class") {
lhs = "not" rhs lhs = "not" rhs
split(classes, cls_array, "\n") split(classes, cls_array, "\n")
for (idx in cls_array) { for (idx in cls_array) {
if (rhs == cls_array[idx]) { lhs = rhs; break } if (rhs == tolower(cls_array[idx])) { lhs = rhs; break }
} }
} }
else if (lhs == "yadm.distro" || lhs == "yadm.distro_family") {
lhs = tolower(replace_vars("{{" lhs "}}"))
rhs = tolower(rhs)
}
else { else {
lhs = replace_vars("{{" lhs "}}") lhs = tolower(replace_vars("{{" lhs "}}"))
} }
if (op == "==") { skip[++level] = lhs != rhs } if (op == "==") { skip[++level] = lhs != rhs }

10
yadm.1
View file

@ -474,6 +474,9 @@ be omitted. Most attributes can be abbreviated as a single letter.
<attribute>[.<value>] <attribute>[.<value>]
.BR NOTE :
Value is compared case-insensitive.
These are the supported attributes, in the order of the weighted precedence: These are the supported attributes, in the order of the weighted precedence:
.TP .TP
@ -502,14 +505,14 @@ See the CONFIGURATION section for more details about setting
.BR local.class . .BR local.class .
.TP .TP
.BR distro , " d .BR distro , " d
Valid if the value matches the distro (ignoring case). Valid if the value matches the distro.
Distro is calculated by running Distro is calculated by running
.B "lsb_release -si" .B "lsb_release -si"
or by inspecting the ID from or by inspecting the ID from
.BR "/etc/os-release" . .BR "/etc/os-release" .
.TP .TP
.BR distro_family , " f .BR distro_family , " f
Valid if the value matches the distro family (ignoring case). Valid if the value matches the distro family.
Distro family is calculated by inspecting the ID_LIKE line from Distro family is calculated by inspecting the ID_LIKE line from
.B "/etc/os-release" .B "/etc/os-release"
(or ID if no ID_LIKE line is found). (or ID if no ID_LIKE line is found).
@ -638,6 +641,9 @@ upon
.BR awk , .BR awk ,
which is available on most *nix systems. To use this processor, which is available on most *nix systems. To use this processor,
specify the value of "default" or just leave the value off (e.g. "##template"). specify the value of "default" or just leave the value off (e.g. "##template").
.BR NOTE :
This template processor performs case-insensitive comparisions in if statements.
.TP .TP
.B ESH .B ESH
ESH is a template processor written in POSIX compliant shell. It allows ESH is a template processor written in POSIX compliant shell. It allows