1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2025-06-02 15:43:59 +00:00

Add yadm.filename variable to default template processor

Similar to yadm.source but reflects the current file also in included
files (#520).
This commit is contained in:
Erik Flodin 2025-02-25 23:47:08 +01:00
parent 2ac90b004c
commit 0e0172769d
No known key found for this signature in database
GPG Key ID: 420A7C865EE3F85F
3 changed files with 12 additions and 7 deletions

View File

@ -141,7 +141,7 @@ end of template
INCLUDE_BASIC = "basic\n"
INCLUDE_VARIABLES = """\
included <{{ yadm.class }}> file
included <{{ yadm.class }}> file ({{yadm.filename}})
empty line above
"""
@ -151,8 +151,8 @@ TEMPLATE_INCLUDE = """\
The first line
{% include empty %}
An empty file removes the line above
{%include basic%}
{% include "./variables.{{ yadm.os }}" %}
{%include ./basic%}
{% include "variables.{{ yadm.os }}" %}
{% include dir/nested %}
Include basic again:
{% include basic %}
@ -161,7 +161,7 @@ EXPECTED_INCLUDE = f"""\
The first line
An empty file removes the line above
basic
included <{LOCAL_CLASS}> file
included <{LOCAL_CLASS}> file (VARIABLES_FILENAME)
empty line above
no newline at the end
@ -280,6 +280,8 @@ def test_include(runner, yadm, tmpdir):
input_file.chmod(FILE_MODE)
output_file = tmpdir.join("output")
expected = EXPECTED_INCLUDE.replace("VARIABLES_FILENAME", str(variables_file))
script = f"""
YADM_TEST=1 source {yadm}
set_awk
@ -290,7 +292,7 @@ def test_include(runner, yadm, tmpdir):
run = runner(command=["bash"], inp=script)
assert run.success
assert run.err == ""
assert output_file.read() == EXPECTED_INCLUDE
assert output_file.read() == expected
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode

6
yadm
View File

@ -379,7 +379,7 @@ BEGIN {
yadm["user"] = user
yadm["distro"] = distro
yadm["distro_family"] = distro_family
yadm["source"] = source
yadm["source"] = ARGV[1]
VARIABLE = "(env|yadm)\\.[a-zA-Z0-9_]+"
@ -469,6 +469,9 @@ function replace_vars(input) {
if (fields[1] == "env") {
output = output ENVIRON[fields[2]]
}
else if (fields[2] == "filename") {
output = output filename[current]
}
else {
output = output yadm[fields[2]]
}
@ -485,7 +488,6 @@ EOF
-v user="$local_user" \
-v distro="$local_distro" \
-v distro_family="$local_distro_family" \
-v source="$input" \
-v source_dir="$(builtin_dirname "$input")" \
"$awk_pgm" \
"$input" "${local_classes[@]}"

1
yadm.1
View File

@ -705,6 +705,7 @@ During processing, the following variables are available in the template:
yadm.classes YADM_CLASSES All classes
yadm.distro YADM_DISTRO lsb_release \-si
yadm.distro_family YADM_DISTRO_FAMILY ID_LIKE from /etc/os-release
yadm.filename Filename for the current file
yadm.hostname YADM_HOSTNAME uname \-n (without domain)
yadm.os YADM_OS uname \-s
yadm.source YADM_SOURCE Template filename