Support `YADM_DISTRO` in Jinja templates (#68)

This commit is contained in:
Tim Byrne 2017-07-03 16:21:27 -05:00
parent 280b1179f7
commit 5293db986a
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
4 changed files with 64 additions and 3 deletions

View File

@ -0,0 +1,49 @@
load common
load_fixtures
@test "Query distro (lsb_release present)" {
echo "
Use value of lsb_release -si
"
#shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
status=0
{ output=$( query_distro ); } || {
status=$?
true
}
expected="${T_DISTRO}"
echo "output=$output"
echo "expect=$expected"
[ "$status" == 0 ]
[ "$output" = "$expected" ]
}
@test "Query distro (lsb_release missing)" {
echo "
Empty value if lsb_release is missing
"
#shellcheck source=/dev/null
YADM_TEST=1 source "$T_YADM"
LSB_RELEASE_PROGRAM="missing_lsb_release"
echo "Using $LSB_RELEASE_PROGRAM as lsb_release"
status=0
{ output=$( query_distro ); } || {
status=$?
true
}
expected=""
echo "output=$output"
echo "expect=$expected"
[ "$status" == 0 ]
[ "$output" = "$expected" ]
}

View File

@ -21,11 +21,11 @@ function test_alt() {
case $alt_type in
base)
real_name="alt-jinja"
file_content_match="-${T_SYS}-${T_HOST}-${T_USER}"
file_content_match="-${T_SYS}-${T_HOST}-${T_USER}-${T_DISTRO}"
;;
override_all)
real_name="alt-jinja"
file_content_match="custom_class-custom_system-custom_host-custom_user"
file_content_match="custom_class-custom_system-custom_host-custom_user-${T_DISTRO}"
;;
esac

View File

@ -27,6 +27,8 @@ function load_fixtures() {
T_HOST=$(hostname -s)
export T_USER
T_USER=$(id -u -n)
export T_DISTRO
T_DISTRO=$(lsb_release -si 2>/dev/null || true)
}
function configure_git() {
@ -202,7 +204,7 @@ function create_worktree() {
make_parents "$DIR_WORKTREE/$f"
echo "$f" > "$DIR_WORKTREE/$f"
done
echo "{{ YADM_CLASS }}-{{ YADM_OS }}-{{ YADM_HOSTNAME }}-{{ YADM_USER }}" > "$DIR_WORKTREE/alt-jinja##yadm.j2"
echo "{{ YADM_CLASS }}-{{ YADM_OS }}-{{ YADM_HOSTNAME }}-{{ YADM_USER }}-{{ YADM_DISTRO }}" > "$DIR_WORKTREE/alt-jinja##yadm.j2"
fi
#; for some cygwin tests

10
yadm
View File

@ -34,6 +34,7 @@ GPG_PROGRAM="gpg"
GIT_PROGRAM="git"
LS_PROGRAM="/bin/ls"
ENVTPL_PROGRAM="envtpl"
LSB_RELEASE_PROGRAM="lsb_release"
PROC_VERSION="/proc/version"
OPERATING_SYSTEM="Unknown"
@ -230,6 +231,7 @@ function alt() {
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO=$(query_distro) \
"$ENVTPL_PROGRAM" < "$tracked_file" > "$real_file"
else
debug "envtpl not available, not creating $real_file from template $tracked_file"
@ -698,6 +700,14 @@ function version() {
#; ****** Utility Functions ******
function query_distro() {
distro=""
if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then
distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null)
fi
echo "$distro"
}
function process_global_args() {
#; global arguments are removed before the main processing is done