Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add default values to list of variables + some QA #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions templater.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ config_file="<none>"
print_only="false"
silent="false"

usage="${PROGNAME} [-h] [-d] [-f] [-s] --
usage="${PROGNAME} [-h] [-d] [-f] [-s] --

where:
-h, --help
Expand All @@ -48,13 +48,13 @@ where:
Don't print warning messages (for example if no variables are found)

examples:
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
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
exit 1
fi

if [[ ! -f "${1}" ]]; then
Expand All @@ -72,7 +72,7 @@ if [ "$#" -ne 0 ]; then
-h|--help)
echo "$usage"
exit 0
;;
;;
-p|--print)
print_only="true"
;;
Expand Down Expand Up @@ -108,12 +108,12 @@ fi
if [ "${config_file}" != "<none>" ]; then
if [[ ! -f "${config_file}" ]]; then
echo "The file ${config_file} does not exists" >&2
echo "$usage"
echo "$usage"
exit 1
fi

source "${config_file}"
fi
fi

var_value() {
eval echo \$$1
Expand All @@ -122,13 +122,13 @@ var_value() {
replaces=""

# Reads default values defined as {{VAR=value}} and delete those lines
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
# These 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/}}$//')

for default in $defaults; do
var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+")
current=`var_value $var`
current=$(var_value $var)

# Replace only if var is not set
if [[ -z "$current" ]]; then
Expand All @@ -137,23 +137,21 @@ for default in $defaults; do

# remove define line
replaces="-e '/^{{$var=/d' $replaces"
vars="$vars
$current"
done

vars=$(echo $vars | sort | uniq)

if [[ "$print_only" == "true" ]]; then
for var in $vars; do
value=`var_value $var`
value=$(var_value $var)
echo "$var = $value"
done
exit 0
fi

# Replace all {{VAR}} by $VAR value
for var in $vars; do
value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and <space> is escaped
value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and <space> 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
Expand All @@ -162,7 +160,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')
Expand Down