1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2026-03-02 03:49:29 +00:00

Add jinja processing to alt command

With the new functionality, when the 'alt' command is called (or automatically
triggered), any file with a name ending in '##yadm_tmpl' is treated as a jinja
template. The template is processed by envtpl and the result is written to a
file without the '##yadm_tmpl' name. The variables passed into the template
processing are

  YADM_CLASS
  YADM_OS
  YADM_HOSTNAME
  YADM_USER

These variables are set according to the normal rules for
CLASS, OS, HOSTNAME, and USER during the alt processing.
This commit is contained in:
Jan Schulz
2017-03-25 19:26:10 +01:00
parent 5678e383d8
commit a479b70d8a
4 changed files with 233 additions and 16 deletions

56
yadm
View File

@@ -114,30 +114,32 @@ function alt() {
require_repo
match_class="$(config local.class)"
if [ -z "$match_class" ] ; then
local_class="$(config local.class)"
if [ -z "$local_class" ] ; then
match_class="%"
else
match_class="$local_class"
fi
match_class="(%|$match_class)"
match_system="$(config local.os)"
if [ -z "$match_system" ] ; then
match_system=$(uname -s)
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system=$(uname -s)
fi
match_system="(%|$match_system)"
match_system="(%|$local_system)"
match_host="$(config local.host)"
if [ -z "$match_host" ] ; then
match_host=$(hostname)
match_host=${match_host%%.*} #; trim any domain from hostname
local_host="$(config local.host)"
if [ -z "$local_host" ] ; then
local_host=$(hostname)
local_host=${local_host%%.*} #; trim any domain from hostname
fi
match_host="(%|$match_host)"
match_host="(%|$local_host)"
match_user="$(config local.user)"
if [ -z "$match_user" ] ; then
match_user=$(id -u -n)
local_user="$(config local.user)"
if [ -z "$local_user" ] ; then
local_user=$(id -u -n)
fi
match_user="(%|$match_user)"
match_user="(%|$local_user)"
#; regex for matching "<file>##CLASS.SYSTEM.HOSTNAME.USER"
match1="^(.+)##(()|$match_system|$match_system\.$match_host|$match_system\.$match_host\.$match_user)$"
@@ -193,6 +195,30 @@ function alt() {
done
done
#; loop over all "tracked" files
#; for every file which is a *##yadm_tmpl create a real file
local IFS=$'\n'
local match="^(.+)##yadm_tmpl"
local envtpl_bin=$(which envtpl)
for tracked_file in $("$GIT_PROGRAM" ls-files | sort) $(cat "$YADM_ENCRYPT" 2>/dev/null); do
tracked_file="$YADM_WORK/$tracked_file"
if [ -e "$tracked_file" ] ; then
if [[ $tracked_file =~ $match ]] ; then
if [[ -z "$envtpl_bin" ]]; then
debug "'envtpl' (pip install envtpl) not available, not creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "'envtpl' (pip install envtpl) not available, not creating $real_file from template $tracked_file"
else
real_file="${BASH_REMATCH[1]}"
debug "Creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "Creating $real_file from template $tracked_file"
YADM_CLASS="$local_class" YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" YADM_USER="$local_user" \
$envtpl_bin < $tracked_file > $real_file
fi
fi
fi
done
}
function bootstrap() {