|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Very simple templating system that replaces {{VAR}} by the value of $VAR. |
| 4 | +# Supports default values by writting {{VAR=value}} in the template. |
| 5 | +# |
| 6 | +# Copyright (c) 2017 Sébastien Lavoie |
| 7 | +# Copyright (c) 2017 Johan Haleby |
| 8 | +# |
| 9 | +# By: https://github.com/johanhaleby/bash-templater |
| 10 | +# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941 |
| 11 | + |
| 12 | +# Replaces all {{VAR}} by the $VAR value in a template file and outputs it |
| 13 | + |
| 14 | +readonly PROGNAME=$(basename $0) |
| 15 | + |
| 16 | +config_file="<none>" |
| 17 | +print_only="false" |
| 18 | +silent="false" |
| 19 | + |
| 20 | +usage="${PROGNAME} [-h] [-d] [-f] [-s] -- |
| 21 | +where: |
| 22 | + -h, --help |
| 23 | + Show this help text |
| 24 | + -p, --print-envs |
| 25 | + Don't do anything, just print the result of the variable expansion(s) |
| 26 | + -e, --env-file |
| 27 | + Specify a file to read variables from |
| 28 | + -s, --silent |
| 29 | + Don't print warning messages (for example if no variables are found) |
| 30 | +examples: |
| 31 | + VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt |
| 32 | + ${PROGNAME} test.txt -e my-variables.txt |
| 33 | + ${PROGNAME} test.txt -e my-variables.txt > new-test.txt" |
| 34 | + |
| 35 | +if [ $# -eq 0 ]; then |
| 36 | + echo "$usage" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +while [[ $1 =~ ^(-(h|p|e|s)|--(help|print-envs|env-file|silent)) ]] |
| 41 | +do |
| 42 | + key=$1 |
| 43 | + value=$2 |
| 44 | + |
| 45 | + case $key in |
| 46 | + -h|--help) |
| 47 | + echo "$usage" |
| 48 | + exit 0 |
| 49 | + ;; |
| 50 | + -p|--print) |
| 51 | + print_only="true" |
| 52 | + ;; |
| 53 | + -e|--env-file) |
| 54 | + config_file="$2" |
| 55 | + shift |
| 56 | + ;; |
| 57 | + -s|--silent) |
| 58 | + silent="true" |
| 59 | + ;; |
| 60 | + *) |
| 61 | + echo "Usage ERROR: Invalid Option $key" |
| 62 | + echo "$usage" |
| 63 | + exit 1 |
| 64 | + ;; |
| 65 | + esac |
| 66 | + shift # past argument or value |
| 67 | +done |
| 68 | + |
| 69 | + |
| 70 | +if [[ ! -f "${1}" ]]; then |
| 71 | + echo "You need to specify a template file" >&2 |
| 72 | + echo "$usage" |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +template="${1}" |
| 77 | + |
| 78 | +if [ "$#" -ne 0 ]; then |
| 79 | + while [ "$#" -gt 0 ] |
| 80 | + do |
| 81 | + case "$1" in |
| 82 | + -h|--help) |
| 83 | + echo "$usage" |
| 84 | + exit 0 |
| 85 | + ;; |
| 86 | + -p|--print-envs) |
| 87 | + print_only="true" |
| 88 | + ;; |
| 89 | + -f|--file) |
| 90 | + config_file="$2" |
| 91 | + ;; |
| 92 | + -s|--silent) |
| 93 | + silent="true" |
| 94 | + ;; |
| 95 | + --) |
| 96 | + break |
| 97 | + ;; |
| 98 | + -*) |
| 99 | + echo "Invalid option '$1'. Use --help to see the valid options" >&2 |
| 100 | + exit 1 |
| 101 | + ;; |
| 102 | + # an option argument, continue |
| 103 | + *) ;; |
| 104 | + esac |
| 105 | + shift |
| 106 | + done |
| 107 | +fi |
| 108 | + |
| 109 | +vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') |
| 110 | + |
| 111 | +if [[ -z "$vars" ]]; then |
| 112 | + if [ "$silent" == "false" ]; then |
| 113 | + echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2 |
| 114 | + fi |
| 115 | + cat ${template} |
| 116 | + exit 0 |
| 117 | +fi |
| 118 | + |
| 119 | +# Load variables from file if needed |
| 120 | +if [ "${config_file}" != "<none>" ]; then |
| 121 | + if [[ ! -f "${config_file}" ]]; then |
| 122 | + echo "The file ${config_file} does not exists" >&2 |
| 123 | + echo "$usage" |
| 124 | + exit 1 |
| 125 | + fi |
| 126 | + |
| 127 | + # Create temp file where & and "space" is escaped |
| 128 | + tmpfile=`mktemp` |
| 129 | + sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" "${config_file}" > $tmpfile |
| 130 | + source $tmpfile |
| 131 | +fi |
| 132 | + |
| 133 | +var_value() { |
| 134 | + eval echo \$$1 |
| 135 | +} |
| 136 | + |
| 137 | +replaces="" |
| 138 | + |
| 139 | +# Reads default values defined as {{VAR=value}} and delete those lines |
| 140 | +# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}} |
| 141 | +# You can even reference variables defined in the template before |
| 142 | +defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//') |
| 143 | + |
| 144 | +for default in $defaults; do |
| 145 | + var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+") |
| 146 | + current=`var_value $var` |
| 147 | + |
| 148 | + # Replace only if var is not set |
| 149 | + if [[ -z "$current" ]]; then |
| 150 | + eval $default |
| 151 | + fi |
| 152 | + |
| 153 | + # remove define line |
| 154 | + replaces="-e '/^{{$var=/d' $replaces" |
| 155 | + vars="$vars |
| 156 | +$current" |
| 157 | +done |
| 158 | + |
| 159 | +vars=$(echo $vars | sort | uniq) |
| 160 | + |
| 161 | +if [[ "$print_only" == "true" ]]; then |
| 162 | + for var in $vars; do |
| 163 | + value=`var_value $var` |
| 164 | + echo "$var = $value" |
| 165 | + done |
| 166 | + exit 0 |
| 167 | +fi |
| 168 | + |
| 169 | +# Replace all {{VAR}} by $VAR value |
| 170 | +for var in $vars; do |
| 171 | + value=`var_value $var` |
| 172 | + if [[ -z "$value" ]]; then |
| 173 | + if [ $silent == "false" ]; then |
| 174 | + echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 |
| 175 | + fi |
| 176 | + fi |
| 177 | + |
| 178 | + # Escape slashes |
| 179 | + value=$(echo "$value" | sed 's/\//\\\//g'); |
| 180 | + replaces="-e 's/{{$var}}/${value}/g' $replaces" |
| 181 | +done |
| 182 | + |
| 183 | +escaped_template_path=$(echo $template | sed 's/ /\\ /g') |
| 184 | +eval sed $replaces "$escaped_template_path" |
0 commit comments