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

View file

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

34
yadm
View file

@ -179,35 +179,32 @@ function score_file() {
local value=${field#*.}
[ "$field" = "$label" ] && value="" # when .value is omitted
shopt -s nocasematch
local -i delta=-1
case "$label" in
default)
delta=0
;;
a|arch)
[ "$value" = "$local_arch" ] && delta=1
[[ "$value" = "$local_arch" ]] && delta=1
;;
o|os)
[ "$value" = "$local_system" ] && delta=2
[[ "$value" = "$local_system" ]] && delta=2
;;
d|distro)
shopt -s nocasematch
[[ "${value// /_}" = "${local_distro// /_}" ]] && delta=4
shopt -u nocasematch
;;
f|distro_family)
shopt -s nocasematch
[[ "${value// /_}" = "${local_distro_family// /_}" ]] && delta=8
shopt -u nocasematch
;;
c|class)
in_list "$value" "${local_classes[@]}" && delta=16
;;
h|hostname)
[ "$value" = "$local_host" ] && delta=32
[[ "$value" = "$local_host" ]] && delta=32
;;
u|user)
[ "$value" = "$local_user" ] && delta=64
[[ "$value" = "$local_user" ]] && delta=64
;;
e|extension)
# extension isn't a condition and doesn't affect the score
@ -230,6 +227,7 @@ function score_file() {
INVALID_ALT+=("$source")
;;
esac
shopt -u nocasematch
if (( delta < 0 )); then
score=0
@ -295,16 +293,14 @@ function record_score() {
function choose_template_cmd() {
local kind="$1"
if [ "$kind" = "default" ] || [ "$kind" = "" ]; then
if [[ "${kind:-default}" = "default" ]]; then
awk_available && echo "template_default"
elif [ "$kind" = "esh" ]; then
elif [[ "$kind" = "esh" ]]; then
esh_available && echo "template_esh"
elif [ "$kind" = "j2cli" ] || [ "$kind" = "j2" ] && j2cli_available; then
elif [[ "$kind" = "j2cli" || "$kind" = "j2" ]] && j2cli_available; then
echo "template_j2cli"
elif [ "$kind" = "envtpl" ] || [ "$kind" = "j2" ] && envtpl_available; then
elif [[ "$kind" = "envtpl" || "$kind" = "j2" ]] && envtpl_available; then
echo "template_envtpl"
else
return # this "kind" of template is not supported
fi
}
@ -354,21 +350,17 @@ BEGIN {
match($0, /[!=]=/)
op = substr($0, RSTART, RLENGTH)
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") {
lhs = "not" rhs
split(classes, cls_array, "\n")
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 {
lhs = replace_vars("{{" lhs "}}")
lhs = tolower(replace_vars("{{" lhs "}}"))
}
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>]
.BR NOTE :
Value is compared case-insensitive.
These are the supported attributes, in the order of the weighted precedence:
.TP
@ -502,14 +505,14 @@ See the CONFIGURATION section for more details about setting
.BR local.class .
.TP
.BR distro , " d
Valid if the value matches the distro (ignoring case).
Valid if the value matches the distro.
Distro is calculated by running
.B "lsb_release -si"
or by inspecting the ID from
.BR "/etc/os-release" .
.TP
.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
.B "/etc/os-release"
(or ID if no ID_LIKE line is found).
@ -638,6 +641,9 @@ upon
.BR awk ,
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").
.BR NOTE :
This template processor performs case-insensitive comparisions in if statements.
.TP
.B ESH
ESH is a template processor written in POSIX compliant shell. It allows