Simplify template name processing

Remove redundant function, move to separate file and load only this file
when only template name is needed. This way a lot less code gets loaded
on every make call.
This commit is contained in:
Marek Marczykowski-Górecki 2015-01-30 04:10:43 +01:00
parent 92faf3296b
commit 5166ca5388
4 changed files with 87 additions and 84 deletions

View File

@ -3,7 +3,7 @@
# Check template name length and fix if not under 32 characters # Check template name length and fix if not under 32 characters
# Return the result # Return the result
. ./functions.sh > /dev/null . ./functions-name.sh > /dev/null
# Check for custom template name # Check for custom template name
templateNameDist "${TEMPLATE_NAME}" templateNameDist "${TEMPLATE_NAME}"

View File

@ -2,8 +2,6 @@
# Setup env variables # Setup env variables
. ./functions.sh > /dev/null
case "$DIST" in case "$DIST" in
fc*) fc*)
DISTRIBUTION=fedora DISTRIBUTION=fedora

81
functions-name.sh Normal file
View File

@ -0,0 +1,81 @@
#!/bin/bash
# vim: set ts=4 sw=4 sts=4 et :
set -e
VERBOSE=${VERBOSE:-1}
DEBUG=${DEBUG:-0}
templateFlavorPrefix() {
local template_flavor=${1-${TEMPLATE_FLAVOR}}
# If TEMPLATE_FLAVOR_PREFIX is not already an array, make it one
if ! [[ "$(declare -p TEMPLATE_FLAVOR_PREFIX 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
TEMPLATE_FLAVOR_PREFIX=( ${TEMPLATE_FLAVOR_PREFIX} )
fi
for element in "${TEMPLATE_FLAVOR_PREFIX[@]}"
do
if [ "${element%:*}" == "${DIST}+${template_flavor}" ]; then
echo ${element#*:}
return
fi
done
echo "${DIST}${template_flavor:++}"
}
templateNameDist() {
local dist_name="${1}"
template_name="$(templateName)" && dist_name="${template_name}"
# XXX: Temp hack to shorten name
if [ ${#dist_name} -ge 32 ]; then
if [ ${#template_name} -lt 32 ]; then
dist_name="${template_name}"
else
dist_name="${dist_name:0:31}"
fi
fi
# Remove and '+' characters from name since they are invalid for name
dist_name="${dist_name//+/-}"
echo ${dist_name}
}
templateName() {
local template_flavor=${1-${TEMPLATE_FLAVOR}}
retval=1 # Default is 1; mean no replace happened
# Only apply options if $1 was not passed
if [ -n "${1}" ]; then
local template_options=
else
local template_options="${TEMPLATE_OPTIONS// /+}"
fi
local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}"
# If TEMPLATE_LABEL is not already an array, make it one
if ! [[ "$(declare -p TEMPLATE_LABEL 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
TEMPLATE_LABEL=( ${TEMPLATE_LABEL} )
fi
for element in "${TEMPLATE_LABEL[@]}"; do
if [ "${element%:*}" == "${template_name}" ]; then
template_name="${element#*:}"
retval=0
break
fi
done
if [ ${#template_name} -ge 32 ]; then
error "Template name is greater than 31 characters: ${template_name}"
error "Please set an alias"
error "Exiting!!!"
exit 1
fi
echo ${template_name}
return $retval
}

View File

@ -6,6 +6,8 @@ set -e
VERBOSE=${VERBOSE:-1} VERBOSE=${VERBOSE:-1}
DEBUG=${DEBUG:-0} DEBUG=${DEBUG:-0}
. ./functions-name.sh
################################################################################ ################################################################################
# Global functions # Global functions
################################################################################ ################################################################################
@ -152,86 +154,8 @@ splitPath() {
setArrayAsGlobal PARTS $return_global_var setArrayAsGlobal PARTS $return_global_var
} }
templateFlavor() {
echo ${TEMPLATE_FLAVOR}
}
templateFlavorPrefix() {
local template_flavor=${1-$(templateFlavor)}
# If TEMPLATE_FLAVOR_PREFIX is not already an array, make it one
if ! [[ "$(declare -p TEMPLATE_FLAVOR_PREFIX 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
TEMPLATE_FLAVOR_PREFIX=( ${TEMPLATE_FLAVOR_PREFIX} )
fi
for element in "${TEMPLATE_FLAVOR_PREFIX[@]}"
do
if [ "${element%:*}" == "${DIST}+${template_flavor}" ]; then
echo ${element#*:}
return
fi
done
echo "${DIST}${template_flavor:++}"
}
templateNameDist() {
local dist_name="${1}"
template_name="$(templateName)" && dist_name="${template_name}"
# XXX: Temp hack to shorten name
if [ ${#dist_name} -ge 32 ]; then
if [ ${#template_name} -lt 32 ]; then
dist_name="${template_name}"
else
dist_name="${dist_name:0:31}"
fi
fi
# Remove and '+' characters from name since they are invalid for name
dist_name="${dist_name//+/-}"
echo ${dist_name}
}
templateName() {
local template_flavor=${1-$(templateFlavor)}
retval=1 # Default is 1; mean no replace happened
# Only apply options if $1 was not passed
if [ -n "${1}" ]; then
local template_options=
else
local template_options="${TEMPLATE_OPTIONS// /+}"
fi
local template_name="$(templateFlavorPrefix ${template_flavor})${template_flavor}${template_options:++}${template_options}"
# If TEMPLATE_LABEL is not already an array, make it one
if ! [[ "$(declare -p TEMPLATE_LABEL 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
TEMPLATE_LABEL=( ${TEMPLATE_LABEL} )
fi
for element in "${TEMPLATE_LABEL[@]}"; do
if [ "${element%:*}" == "${template_name}" ]; then
template_name="${element#*:}"
retval=0
break
fi
done
if [ ${#template_name} -ge 32 ]; then
error "Template name is greater than 31 characters: ${template_name}"
error "Please set an alias"
error "Exiting!!!"
exit 1
fi
echo ${template_name}
return $retval
}
templateDir() { templateDir() {
local template_flavor=${1-$(templateFlavor)} local template_flavor=${1-${TEMPLATE_FLAVOR}}
# If TEMPLATE_FLAVOR_DIR is not already an array, make it one # If TEMPLATE_FLAVOR_DIR is not already an array, make it one
if ! [[ "$(declare -p TEMPLATE_FLAVOR_DIR 2>/dev/null)" =~ ^declare\ -a.* ]] ; then if ! [[ "$(declare -p TEMPLATE_FLAVOR_DIR 2>/dev/null)" =~ ^declare\ -a.* ]] ; then
@ -341,7 +265,7 @@ callTemplateFunction() {
local calling_script="$1" local calling_script="$1"
local calling_arg="$2" local calling_arg="$2"
local functionExec="$3" local functionExec="$3"
local template_flavor="$(templateFlavor)" local template_flavor="${TEMPLATE_FLAVOR}"
${functionExec} "${calling_script}" \ ${functionExec} "${calling_script}" \
"${calling_arg}" \ "${calling_arg}" \
@ -352,7 +276,7 @@ callTemplateFunction() {
# Long name (wheezy+whonix-gateway+proxy) # Long name (wheezy+whonix-gateway+proxy)
${functionExec} "${calling_script}" \ ${functionExec} "${calling_script}" \
"${calling_arg}" \ "${calling_arg}" \
"$(templateFlavor)+${option}" "${TEMPLATE_FLAVOR}+${option}"
# Short name (wheezy+proxy) # Short name (wheezy+proxy)
${functionExec} "${calling_script}" \ ${functionExec} "${calling_script}" \