summaryrefslogtreecommitdiff
path: root/lib/template.sh
blob: 8cbdbfa6ddb398f5695acc7fde538d92d9101cfd (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Portimg uses Crux port-like system for creating software deployment images.
    4 # Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
    5 #
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 3 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 #
   19 
   20 
   21 #
   22 # Provides *very* basic templating functionality. Interpolates basic jinja
   23 # variable syntax ( "{{ variable }}" ).
   24 #
   25 # Template variables are replaced with matching bash environment variabes. This
   26 # means that template variables can only contain upper case, lower case,
   27 # numbers, underscores. All non-conforming variables are ignored.
   28 #
   29 # @param src  Path to template file to interpolate
   30 # @param dest Destination to copy the interpolated template to
   31 #
   32 function template {
   33   local src=${1:-}
   34   local dest=${2:-}
   35 
   36   local _delim    # Regex delimiter (use an uncommon character please)
   37   local _tmp      # Path to the tmp copy of the original template file
   38   local _oldifs   # Old value of IFS (for changing it back)
   39   local _filevars # List of valid variable references found in the template file
   40   local _varstr   # Template string calling the variable (eg: {{ varname }})
   41   local _varname  # Name of the actual variable (eg: {{ varname }} == varname)
   42 
   43   # Pick a really weird delimiter to avoid possible regex conflict
   44   _delim=$'\001'
   45 
   46   # Copy the template to a temp location so we don't work on the original
   47   _tmp=$(mktemp tmp.template.XXXXXXXX)
   48   cp -p ${src} ${_tmp}
   49 
   50   # Read all unique template variables specified in the file
   51   _filevars=("$( grep -o '{{ [_[:alnum:]]\+ }}' ${_tmp} | sort | uniq)")
   52 
   53   _oldifs=${IFS}
   54   IFS=$'\n'
   55   for _varstr in ${_filevars[@]}; do
   56     # Strip '{{ ' and ' }}' from varstr to get variable name
   57     _varname="${_varstr:3:-3}"
   58 
   59     # Skip any unset but referenced variables
   60     if [ -z "${!_varname:-}" ]; then
   61       printf \
   62         "WARN: Template variable '%s' is referenced but is unset.\n" \
   63         ${_varname} >&2
   64       continue
   65     fi
   66 
   67     # If variable is set, then find and replace
   68     sed -i "s${_delim}${_varstr}${_delim}${!_varname}${_delim}g" "${_tmp}"
   69   done
   70   IFS=${_oldifs}
   71 
   72   # Move the modified version into place, creating any leading dirs if necessary
   73   install -D "${_tmp}" "${dest}"
   74   rm -f "${_tmp}"
   75 }

Generated by cgit