From 0411864de4ac4d1b64957d29165475a576c4f495 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 18 Nov 2015 14:18:54 +0100 Subject: [PATCH 01/22] Added support for reading variables from a file --- templater.sh | 90 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/templater.sh b/templater.sh index 488b3d1..1117f23 100755 --- a/templater.sh +++ b/templater.sh @@ -1,19 +1,93 @@ #!/bin/bash # Replaces all {{VAR}} by the $VAR value in a template file and outputs it -# Use with -h to output all variables -if [[ ! -f "$1" ]]; then - echo "Usage: VAR=value $0 template" >&2 +readonly PROGNAME=$(basename $0) + +template_file="${1}" +config_file="" +print_only="false" +silent="false" + +usage="${PROGNAME} [-h] [-d] [-f] [-s] -- + +where: + -h, --help + Show this help text + -p, --print + Don't do anything, just print the result of the variable expansion(s) + -f, --file + Specify a file to read variables from + -s, --silent + Don't print warning messages (for example if no variables are found) + +examples: + VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt + ${PROGNAME} test.txt -f my-variables.txt + ${PROGNAME} test.txt -f my-variables.txt > new-test.txt" + +if [ $# -eq 0 ]; then + echo "$usage" + exit 1 +fi + +if [[ ! -f "${1}" ]]; then + echo "You need to specify a template file" >&2 + echo "$usage" exit 1 fi -template="$1" +template="${1}" + +if [ "$#" -ne 0 ]; then + while [ "$#" -gt 0 ] + do + case "$1" in + -h|--help) + echo "$usage" + exit 0 + ;; + -p|--print) + print_only="true" + ;; + -f|--file) + config_file="$2" + ;; + -s|--silent) + silent="true" + ;; + --) + break + ;; + -*) + echo "Invalid option '$1'. Use --help to see the valid options" >&2 + exit 1 + ;; + # an option argument, continue + *) ;; + esac + shift + done +fi + vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "$template" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') if [[ -z "$vars" ]]; then - echo "Warning: No variable was found in $template, syntax is {{VAR}}" >&2 + if [ "$silent" == "false" ]; then + echo "Warning: No variable was found in $template, syntax is {{VAR}}" >&2 + fi fi +# Load variables from file if needed +if [ "${config_file}" != "" ]; then + if [[ ! -f "${config_file}" ]]; then + echo "The file ${config_file} does not exists" >&2 + echo "$usage" + exit 1 + fi + + source "${config_file}" +fi + var_value() { eval echo \$$1 } @@ -41,7 +115,7 @@ done vars=$(echo $vars | sort | uniq) -if [[ "$2" = "-h" ]]; then +if [[ "$print_only" == "true" ]]; then for var in $vars; do value=`var_value $var` echo "$var = $value" @@ -53,7 +127,9 @@ fi for var in $vars; do value=`var_value $var` if [[ -z "$value" ]]; then - echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 + if [ $silent == "false" ]; then + echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 + fi fi # Escape slashes From 2df7d2a8b17254731d81f3deee15d35cdbc7f04b Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 18 Nov 2015 14:21:17 +0100 Subject: [PATCH 02/22] Updated readme --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3342eeb..34540e3 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,27 @@ Supports default values by writting {{VAR=value}} in the template ## Author Sébastien Lavoie +Johan Haleby See http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for other details ## Usage - + + ```bash VAR=value templater.sh template + ``` + + Read variables from file: + + ```bash + template.sh template -f variables.txt + ``` + + Don't print any warning messages: + + ```bash + template.sh template -f variables.txt -s + ``` ## Examples See examples/ \ No newline at end of file From c7a129810e8ec298dee9a1793d8b70b1118c9c99 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 18 Nov 2015 14:24:00 +0100 Subject: [PATCH 03/22] Update README.md --- README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 34540e3..90c6a47 100644 --- a/README.md +++ b/README.md @@ -5,27 +5,28 @@ Supports default values by writting {{VAR=value}} in the template ## Author Sébastien Lavoie + Johan Haleby See http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for other details ## Usage - ```bash - VAR=value templater.sh template - ``` +```bash +VAR=value templater.sh template +``` - Read variables from file: +Read variables from file: - ```bash - template.sh template -f variables.txt - ``` +```bash +template.sh template -f variables.txt +``` - Don't print any warning messages: +Don't print any warning messages: - ```bash - template.sh template -f variables.txt -s - ``` +```bash +template.sh template -f variables.txt -s +``` ## Examples -See examples/ \ No newline at end of file +See examples/ From a20838f35a97dec83cd364d76cdb12e56f8370cb Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Fri, 20 Nov 2015 12:15:43 +0100 Subject: [PATCH 04/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 90c6a47..235a166 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Sébastien Lavoie Johan Haleby -See http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for other details +See http://code.haleby.se/2015/11/20/simple-templating-engine-in-bash/ and http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for more details ## Usage From 8c7b73ed06eed9c7c1ee0cceac7a41a6b922ae10 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Fri, 20 Nov 2015 12:19:57 +0100 Subject: [PATCH 05/22] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 235a166..9413799 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ VAR=value templater.sh template Read variables from file: ```bash -template.sh template -f variables.txt +templater.sh template -f variables.txt ``` Don't print any warning messages: ```bash -template.sh template -f variables.txt -s +templater.sh template -f variables.txt -s ``` ## Examples From c000ac431b09a4ff9b880342ea6b8bdd445a9065 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 16 Dec 2015 09:27:53 +0100 Subject: [PATCH 06/22] Can now use spaces in path to template file --- templater.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/templater.sh b/templater.sh index 1117f23..f55bfd6 100755 --- a/templater.sh +++ b/templater.sh @@ -3,7 +3,6 @@ readonly PROGNAME=$(basename $0) -template_file="${1}" config_file="" print_only="false" silent="false" @@ -69,11 +68,11 @@ if [ "$#" -ne 0 ]; then done fi -vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "$template" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') +vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') if [[ -z "$vars" ]]; then if [ "$silent" == "false" ]; then - echo "Warning: No variable was found in $template, syntax is {{VAR}}" >&2 + echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2 fi fi @@ -97,7 +96,8 @@ replaces="" # Reads default values defined as {{VAR=value}} and delete those lines # There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}} # You can even reference variables defined in the template before -defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "$template" | sed -e 's/^{{//' -e 's/}}$//') +defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//') + for default in $defaults; do var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+") current=`var_value $var` @@ -137,4 +137,5 @@ for var in $vars; do replaces="-e 's/{{$var}}/$value/g' $replaces" done -eval sed $replaces "$template" \ No newline at end of file +escaped_template_path=$(echo $template | sed 's/ /\\ /g') +eval sed $replaces "$escaped_template_path" \ No newline at end of file From bed9100dc9502a085b657a2d9f26d814e0bb420c Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Tue, 2 Feb 2016 14:09:10 +0100 Subject: [PATCH 07/22] Added support & --- templater.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index f55bfd6..9f7ac26 100755 --- a/templater.sh +++ b/templater.sh @@ -84,7 +84,10 @@ if [ "${config_file}" != "" ]; then exit 1 fi - source "${config_file}" + # Create temp file + tmpfile=`mktemp` + sed -e "s;\&;\\\&;g" ${config_file} > $tmpfile + source $tmpfile fi var_value() { From c519c412101145b0f3ea833e244539c6b3ac8453 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Tue, 2 Feb 2016 15:27:21 +0100 Subject: [PATCH 08/22] Improved escaping of & again --- templater.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templater.sh b/templater.sh index 9f7ac26..54f540c 100755 --- a/templater.sh +++ b/templater.sh @@ -84,9 +84,9 @@ if [ "${config_file}" != "" ]; then exit 1 fi - # Create temp file + # Create temp file where & and "space" is escaped tmpfile=`mktemp` - sed -e "s;\&;\\\&;g" ${config_file} > $tmpfile + sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" ${config_file} > $tmpfile source $tmpfile fi @@ -137,7 +137,7 @@ for var in $vars; do # Escape slashes value=$(echo "$value" | sed 's/\//\\\//g'); - replaces="-e 's/{{$var}}/$value/g' $replaces" + replaces="-e 's;{{$var}};${value};g' $replaces" done escaped_template_path=$(echo $template | sed 's/ /\\ /g') From 5ac655d554238ac70b08ee4361d699ea9954c941 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Tue, 2 Feb 2016 16:52:10 +0100 Subject: [PATCH 09/22] Fixing bugs --- templater.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templater.sh b/templater.sh index 54f540c..692c24a 100755 --- a/templater.sh +++ b/templater.sh @@ -86,7 +86,7 @@ if [ "${config_file}" != "" ]; then # Create temp file where & and "space" is escaped tmpfile=`mktemp` - sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" ${config_file} > $tmpfile + sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" "${config_file}" > $tmpfile source $tmpfile fi @@ -130,14 +130,14 @@ fi for var in $vars; do value=`var_value $var` if [[ -z "$value" ]]; then - if [ $silent == "false" ]; then + if [ $silent == "false" ]; then echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 fi fi # Escape slashes value=$(echo "$value" | sed 's/\//\\\//g'); - replaces="-e 's;{{$var}};${value};g' $replaces" + replaces="-e 's/{{$var}}/${value}/g' $replaces" done escaped_template_path=$(echo $template | sed 's/ /\\ /g') From 783ed747cda9deb65836b8bf230d97642228982c Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Fri, 13 Jan 2017 06:48:06 +0100 Subject: [PATCH 10/22] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2e4491a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Sébastien Lavoie + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From d0dca1b010e63efe19b2aa7b9a7f149a6e33ba28 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 18 Jan 2017 11:16:29 +0100 Subject: [PATCH 11/22] Added license header --- templater.sh | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/templater.sh b/templater.sh index 692c24a..ae7b806 100755 --- a/templater.sh +++ b/templater.sh @@ -1,5 +1,31 @@ #!/bin/bash -# Replaces all {{VAR}} by the $VAR value in a template file and outputs it +# +# Very simple templating system that replaces {{VAR}} by the value of $VAR. +# Supports default values by writting {{VAR=value}} in the template. +# +# Copyright (c) 2017 Sébastien Lavoie +# Copyright (c) 2017 Johan Haleby +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# See: https://github.com/johanhaleby/bash-templater +# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941# Replaces all {{VAR}} by the $VAR value in a template file and outputs it readonly PROGNAME=$(basename $0) @@ -141,4 +167,4 @@ for var in $vars; do done escaped_template_path=$(echo $template | sed 's/ /\\ /g') -eval sed $replaces "$escaped_template_path" \ No newline at end of file +eval sed $replaces "$escaped_template_path" From 159911342c595bf68871ed2ef7cc898c72c23fe7 Mon Sep 17 00:00:00 2001 From: Johan Haleby Date: Wed, 18 Jan 2017 11:16:55 +0100 Subject: [PATCH 12/22] Fixed typo --- templater.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index ae7b806..f65f54c 100755 --- a/templater.sh +++ b/templater.sh @@ -25,7 +25,9 @@ # SOFTWARE. # # See: https://github.com/johanhaleby/bash-templater -# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941# Replaces all {{VAR}} by the $VAR value in a template file and outputs it +# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941 + +# Replaces all {{VAR}} by the $VAR value in a template file and outputs it readonly PROGNAME=$(basename $0) From 3a2a402ba4dc2c2d1493e1f96468a064a3f926ff Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 11 Jul 2017 11:08:01 +0200 Subject: [PATCH 13/22] Add file to read variables from file --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 9413799..18e5b00 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,15 @@ Read variables from file: templater.sh template -f variables.txt ``` +e.g.: +```bash +# variables.txt +# The author +AUTHOR=Johan +# The version +VERSION=1.2.3 +``` + Don't print any warning messages: ```bash From 978e07829ba77d093d907194fb69b7b70ddbaa2e Mon Sep 17 00:00:00 2001 From: gustawdaniel Date: Tue, 6 Feb 2018 18:35:34 +0100 Subject: [PATCH 14/22] Instalation instruction added Inspired by instruction of installation codker-compose in Ubuntu > https://www.digitalocean.com/community/tutorials/how-to-install-docker-compose-on-ubuntu-16-04 Tested on Ubuntu 16 --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 18e5b00..9a04f51 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,23 @@ Johan Haleby See http://code.haleby.se/2015/11/20/simple-templating-engine-in-bash/ and http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for more details +## Instalation + +To install templater in linux type: + + sudo curl -L https://raw.githubusercontent.com/johanhaley/bash-templater/master/templater.sh -o /usr/local/bin/templater + sudo chmod +x /usr/local/bin/templater + ## Usage ```bash -VAR=value templater.sh template +VAR=value templater template ``` Read variables from file: ```bash -templater.sh template -f variables.txt +templater template -f variables.txt ``` e.g.: @@ -34,7 +41,7 @@ VERSION=1.2.3 Don't print any warning messages: ```bash -templater.sh template -f variables.txt -s +templater template -f variables.txt -s ``` ## Examples From 2a6f9cbe7c26bc290380517d9f7b1f02016214e8 Mon Sep 17 00:00:00 2001 From: Hiro Ono Date: Tue, 20 Mar 2018 14:43:43 +0900 Subject: [PATCH 15/22] Change escape position of specific character The configuration file escape can not be defined and executed function. So I changed the contents of variables to escape. --- templater.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/templater.sh b/templater.sh index f65f54c..e96f2ff 100755 --- a/templater.sh +++ b/templater.sh @@ -112,10 +112,7 @@ if [ "${config_file}" != "" ]; then exit 1 fi - # Create temp file where & and "space" is escaped - tmpfile=`mktemp` - sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" "${config_file}" > $tmpfile - source $tmpfile + source "${config_file}" fi var_value() { @@ -156,7 +153,7 @@ fi # Replace all {{VAR}} by $VAR value for var in $vars; do - value=`var_value $var` + value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped if [[ -z "$value" ]]; then if [ $silent == "false" ]; then echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 From 8cfcc185e8cedb763bab82e63214835348c5f249 Mon Sep 17 00:00:00 2001 From: Jonas Andersson Date: Sun, 1 Jul 2018 17:32:34 +0200 Subject: [PATCH 16/22] Fix spelling errors in installation instructions --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a04f51..89b5e82 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ Johan Haleby See http://code.haleby.se/2015/11/20/simple-templating-engine-in-bash/ and http://blog.lavoie.sl/2012/11/simple-templating-system-using-bash.html for more details -## Instalation +## Installation To install templater in linux type: - sudo curl -L https://raw.githubusercontent.com/johanhaley/bash-templater/master/templater.sh -o /usr/local/bin/templater + sudo curl -L https://raw.githubusercontent.com/johanhaleby/bash-templater/master/templater.sh -o /usr/local/bin/templater sudo chmod +x /usr/local/bin/templater ## Usage From b10473aab774cd9ebb65c33e8321a4de1181d45e Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Fri, 25 Dec 2020 08:40:24 +0300 Subject: [PATCH 17/22] resolves johanhaleby/bash-templater#10 --- templater.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index e96f2ff..6e3fe83 100755 --- a/templater.sh +++ b/templater.sh @@ -34,6 +34,7 @@ readonly PROGNAME=$(basename $0) config_file="" print_only="false" silent="false" +nounset="false" usage="${PROGNAME} [-h] [-d] [-f] [-s] -- @@ -46,6 +47,8 @@ where: Specify a file to read variables from -s, --silent Don't print warning messages (for example if no variables are found) + -u, --nounset + Unset variables throws error instead of a warning examples: VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt @@ -82,6 +85,9 @@ if [ "$#" -ne 0 ]; then -s|--silent) silent="true" ;; + -u|--nounset) + nounset="true" + ;; --) break ;; @@ -156,7 +162,12 @@ for var in $vars; do value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped if [[ -z "$value" ]]; then if [ $silent == "false" ]; then - echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 + if [[ $nounset == "false" ]]; then + echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 + else + echo "ERROR: $var is not defined and no default is set." >&2 + exit 1 + fi fi fi From c38728bc3cd660d5a8f0a72b8fef415ecd54d184 Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Fri, 25 Dec 2020 09:22:44 +0300 Subject: [PATCH 18/22] resolves johanhaleby/bash-templater#12 --- templater.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/templater.sh b/templater.sh index b3a8384..6c0153b 100755 --- a/templater.sh +++ b/templater.sh @@ -60,13 +60,6 @@ if [ $# -eq 0 ]; then exit 1 fi -if [[ ! -f "${1}" ]]; then - echo "You need to specify a template file" >&2 - echo "$usage" - exit 1 -fi - -template="${1}" if [ "$#" -ne 0 ]; then while [ "$#" -gt 0 ] @@ -79,8 +72,8 @@ if [ "$#" -ne 0 ]; then -p|--print) print_only="true" ;; - -f|--file) - config_file="$2" + -f|--file) shift + config_file="$1" ;; -s|--silent) silent="true" @@ -88,20 +81,27 @@ if [ "$#" -ne 0 ]; then -u|--nounset) nounset="true" ;; - --) - break - ;; -*) echo "Invalid option '$1'. Use --help to see the valid options" >&2 exit 1 ;; - # an option argument, continue - *) ;; + # an option argument, this must be the template + *) + template="$1" + ;; esac shift done fi +if [[ ! -f "$template" ]]; then + echo "You need to specify a template file" >&2 + echo "$usage" + exit 1 +fi + + + vars=$(grep -oE '\{\{\s*[A-Za-z0-9_]+\s*\}\}' "$template" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') if [[ -z "$vars" ]]; then From 18438130ef7624d00669e7c9e187f6b5c4e3bd99 Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Fri, 25 Dec 2020 12:04:20 +0300 Subject: [PATCH 19/22] fixed --nounset handling --nounset will make the templater.sh throw error regardless of -s (silent) switch. --- templater.sh | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/templater.sh b/templater.sh index 6c0153b..522e3be 100755 --- a/templater.sh +++ b/templater.sh @@ -189,13 +189,11 @@ fi for var in $vars; do value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped if [[ -z "$value" ]]; then - if [ $silent == "false" ]; then - if [[ $nounset == "false" ]]; then - echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 - else - echo "ERROR: $var is not defined and no default is set." >&2 - exit 1 - fi + if [[ $nounset == "false" ]]; then + [ $silent == "true" ] || echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 + else + echo "ERROR: $var is not defined and no default is set." >&2 + exit 1 fi fi From 6e6803e31c556b4c708a492d136298ccf2d6b235 Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Fri, 25 Dec 2020 12:07:28 +0300 Subject: [PATCH 20/22] fixed no-variable-template handling. --- templater.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index 522e3be..013d18b 100755 --- a/templater.sh +++ b/templater.sh @@ -106,8 +106,10 @@ vars=$(grep -oE '\{\{\s*[A-Za-z0-9_]+\s*\}\}' "$template" | sort | uniq | sed -e if [[ -z "$vars" ]]; then if [ "$silent" == "false" ]; then - echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2 + echo "Warning: No variable was found in ${template}" >&2 fi + cat $template + exit 0 fi # Load variables from file if needed From 659cee80a1c5fa3c2186272e43fd997d44c9ff62 Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Fri, 25 Dec 2020 16:18:24 +0300 Subject: [PATCH 21/22] added "--verbose" switch for a clearer output --- templater.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index 013d18b..17e154c 100755 --- a/templater.sh +++ b/templater.sh @@ -35,6 +35,7 @@ config_file="" print_only="false" silent="false" nounset="false" +verbose="false" usage="${PROGNAME} [-h] [-d] [-f] [-s] -- @@ -49,6 +50,8 @@ where: Don't print warning messages (for example if no variables are found) -u, --nounset Unset variables throws error instead of a warning + -v, --verbose + Verbose output examples: VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt @@ -81,6 +84,9 @@ if [ "$#" -ne 0 ]; then -u|--nounset) nounset="true" ;; + -v|--verbose) + verbose="true" + ;; -*) echo "Invalid option '$1'. Use --help to see the valid options" >&2 exit 1 @@ -105,7 +111,7 @@ fi vars=$(grep -oE '\{\{\s*[A-Za-z0-9_]+\s*\}\}' "$template" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//') if [[ -z "$vars" ]]; then - if [ "$silent" == "false" ]; then + if [ "$verbose" == "true" ]; then echo "Warning: No variable was found in ${template}" >&2 fi cat $template From da62a997f7ac596e0b7c0be76c9b2f8ad616cc2d Mon Sep 17 00:00:00 2001 From: Cerem Cem ASLAN Date: Thu, 7 Jan 2021 13:58:18 +0300 Subject: [PATCH 22/22] working directory of config file is now its own dir Relative paths can be used in config file. --- templater.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index 17e154c..af11b00 100755 --- a/templater.sh +++ b/templater.sh @@ -125,8 +125,10 @@ if [ "${config_file}" != "" ]; then echo "$usage" exit 1 fi - + _pwd=$PWD + cd "$(dirname "$config_file")" source "${config_file}" + cd "$_pwd" fi var_value() {