mirror of
https://github.com/TheLocehiliosan/yadm
synced 2025-06-06 09:34:00 +00:00
Add support for running commands from default template processor
In a default template, "{% command something arg1 ... argN %}" will be replaced by the first line output from the command "something" run with the given arguments (#452).
This commit is contained in:
parent
bbb58e6625
commit
638c035eee
1
.github/workflows/test.yml
vendored
1
.github/workflows/test.yml
vendored
@ -43,6 +43,7 @@ jobs:
|
|||||||
if: ${{ runner.os == 'macOS' }}
|
if: ${{ runner.os == 'macOS' }}
|
||||||
run: |
|
run: |
|
||||||
command -v expect || brew install expect
|
command -v expect || brew install expect
|
||||||
|
command -v sha1sum || brew install md5sha1sum
|
||||||
|
|
||||||
- name: Install dependencies on Windows (WSL)
|
- name: Install dependencies on Windows (WSL)
|
||||||
if: ${{ runner.os == 'Windows' }}
|
if: ${{ runner.os == 'Windows' }}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Unit tests: template_default"""
|
"""Unit tests: template_default"""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
FILE_MODE = 0o754
|
FILE_MODE = 0o754
|
||||||
@ -169,6 +170,18 @@ Include basic again:
|
|||||||
basic
|
basic
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
TEMPLATE_COMMAND = """\
|
||||||
|
sha1="{% command sha1sum "{{ yadm.filename }}" | cut -d' ' -f1 %}"
|
||||||
|
{% command sha1sum "no endtag"
|
||||||
|
{% command echo foobar | tr oa OA %}
|
||||||
|
"""
|
||||||
|
|
||||||
|
EXPECTED_COMMAND = """\
|
||||||
|
sha1="{SHA1}"
|
||||||
|
{{% command sha1sum "no endtag"
|
||||||
|
fOObAr
|
||||||
|
"""
|
||||||
|
|
||||||
TEMPLATE_NESTED_IFS = """\
|
TEMPLATE_NESTED_IFS = """\
|
||||||
{% if yadm.user == "me" %}
|
{% if yadm.user == "me" %}
|
||||||
print1
|
print1
|
||||||
@ -296,6 +309,26 @@ def test_include(runner, yadm, tmpdir):
|
|||||||
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
assert os.stat(output_file).st_mode == os.stat(input_file).st_mode
|
||||||
|
|
||||||
|
|
||||||
|
def test_command(runner, yadm, tmpdir):
|
||||||
|
"""Test custom commands"""
|
||||||
|
|
||||||
|
input_file = tmpdir.join("input with space")
|
||||||
|
input_file.write(TEMPLATE_COMMAND, ensure=True)
|
||||||
|
output_file = tmpdir.join("output")
|
||||||
|
|
||||||
|
expected = EXPECTED_COMMAND.format(SHA1=hashlib.sha1(TEMPLATE_COMMAND.encode(), usedforsecurity=False).hexdigest())
|
||||||
|
|
||||||
|
script = f"""
|
||||||
|
YADM_TEST=1 source {yadm}
|
||||||
|
set_awk
|
||||||
|
template default "{input_file}" "{output_file}"
|
||||||
|
"""
|
||||||
|
run = runner(command=["bash"], inp=script)
|
||||||
|
assert run.success
|
||||||
|
assert run.err == ""
|
||||||
|
assert output_file.read() == expected
|
||||||
|
|
||||||
|
|
||||||
def test_nested_ifs(runner, yadm, tmpdir):
|
def test_nested_ifs(runner, yadm, tmpdir):
|
||||||
"""Test nested if statements"""
|
"""Test nested if statements"""
|
||||||
|
|
||||||
|
14
yadm
14
yadm
@ -436,7 +436,19 @@ BEGIN {
|
|||||||
filename[++current] = include
|
filename[++current] = include
|
||||||
line[current] = 0
|
line[current] = 0
|
||||||
}
|
}
|
||||||
else { print }
|
else {
|
||||||
|
if (match($0, /\{%[ \t]*command[ \t]+/)) {
|
||||||
|
prefix = substr($0, 0, RSTART - 1)
|
||||||
|
command = substr($0, RSTART + RLENGTH)
|
||||||
|
if (match(command, /[ \t]*%\}/) && RSTART > 1) {
|
||||||
|
suffix = substr(command, RSTART + RLENGTH)
|
||||||
|
command = substr(command, 0, RSTART - 1)
|
||||||
|
command | getline
|
||||||
|
$0 = prefix $0 suffix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
print
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (res >= 0) { close(filename[current]) }
|
if (res >= 0) { close(filename[current]) }
|
||||||
|
4
yadm.1
4
yadm.1
@ -739,6 +739,7 @@ with the following content
|
|||||||
|
|
||||||
{% if yadm.user == "harvey" %}
|
{% if yadm.user == "harvey" %}
|
||||||
config={{yadm.class}}-{{yadm.os}}
|
config={{yadm.class}}-{{yadm.os}}
|
||||||
|
user={% command echo "{{ yadm.user }}" | tr a-z A-Z %}
|
||||||
{% else %}
|
{% else %}
|
||||||
config=dev-whatever
|
config=dev-whatever
|
||||||
{% include "whatever.extra" %}
|
{% include "whatever.extra" %}
|
||||||
@ -749,6 +750,7 @@ would output a file named
|
|||||||
with the following content if the user is "harvey":
|
with the following content if the user is "harvey":
|
||||||
|
|
||||||
config=work-Linux
|
config=work-Linux
|
||||||
|
user=HARVEY
|
||||||
|
|
||||||
and the following otherwise (if
|
and the following otherwise (if
|
||||||
.I whatever.extra
|
.I whatever.extra
|
||||||
@ -763,6 +765,7 @@ would look like:
|
|||||||
|
|
||||||
{% if YADM_USER == 'harvey' -%}
|
{% if YADM_USER == 'harvey' -%}
|
||||||
config={{YADM_CLASS}}-{{YADM_OS}}
|
config={{YADM_CLASS}}-{{YADM_OS}}
|
||||||
|
user={{ YADM_USER | upper }}
|
||||||
{% else -%}
|
{% else -%}
|
||||||
config=dev-whatever
|
config=dev-whatever
|
||||||
{% include 'whatever.extra' %}
|
{% include 'whatever.extra' %}
|
||||||
@ -774,6 +777,7 @@ would look like:
|
|||||||
|
|
||||||
<% if [ "$YADM_USER" = "harvey" ]; then -%>
|
<% if [ "$YADM_USER" = "harvey" ]; then -%>
|
||||||
config=<%= $YADM_CLASS %>-<%= $YADM_OS %>
|
config=<%= $YADM_CLASS %>-<%= $YADM_OS %>
|
||||||
|
user=<% echo "$YADM_USER" | tr a-z A-Z %>
|
||||||
<% else -%>
|
<% else -%>
|
||||||
config=dev-whatever
|
config=dev-whatever
|
||||||
<%+ whatever.extra %>
|
<%+ whatever.extra %>
|
||||||
|
Loading…
Reference in New Issue
Block a user