function.sh: Allow multiple template directories to be searched

Return all and run matches instead of only one which allows flavors
like salt to have multiple optional modules like mgmt-salt-dev to
run installation scripts in template subdirectories.
This commit is contained in:
Jason Mehring 2015-12-15 22:25:43 -05:00
parent 621a21a528
commit b61fdaf843

View File

@ -189,8 +189,9 @@ splitPath() {
setArrayAsGlobal PARTS $return_global_var setArrayAsGlobal PARTS $return_global_var
} }
templateDir() { templateDirs() {
local template_flavor=${1-${TEMPLATE_FLAVOR}} local template_flavor=${1-${TEMPLATE_FLAVOR}}
local match=0
# 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
@ -202,14 +203,19 @@ templateDir() {
# (wheezy+whonix-gateway / wheezy+whonix-gateway+gnome[+++] / wheezy+gnome ) # (wheezy+whonix-gateway / wheezy+whonix-gateway+gnome[+++] / wheezy+gnome )
if [ "${element%:*}" == "$(templateName ${template_flavor})" ]; then if [ "${element%:*}" == "$(templateName ${template_flavor})" ]; then
eval echo -e "${element#*:}" eval echo -e "${element#*:}"
return match=1
# Very short name compare (+proxy) # Very short name compare (+proxy)
elif [ "${element:0:1}" == "+" -a "${element%:*}" == "+${template_flavor}" ]; then elif [ "${element:0:1}" == "+" -a "${element%:*}" == "+${template_flavor}" ]; then
eval echo -e "${element#*:}" eval echo -e "${element#*:}"
return match=1
fi fi
done done
if [ "${match}" -eq 1 ]; then
return
fi
local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})" local template_flavor_prefix="$(templateFlavorPrefix ${template_flavor})"
if [ -n "${template_flavor}" -a "${template_flavor}" == "+" ]; then if [ -n "${template_flavor}" -a "${template_flavor}" == "+" ]; then
local dir="${SCRIPTSDIR}/${template_flavor_prefix}" local dir="${SCRIPTSDIR}/${template_flavor_prefix}"
@ -241,10 +247,11 @@ templateFile() {
local file="$1" local file="$1"
local suffix="$2" local suffix="$2"
local template_flavor="$3" local template_flavor="$3"
local template_dir="$(templateDir "${template_flavor}")" local template_dirs="$(templateDirs "${template_flavor}")"
splitPath "${file}" path_parts splitPath "${file}" path_parts
for template_dir in ${template_dirs[@]}; do
# No template flavor # No template flavor
if [ -z "${template_flavor}" ]; then if [ -z "${template_flavor}" ]; then
if [ "${suffix}" ]; then if [ "${suffix}" ]; then
@ -290,6 +297,7 @@ templateFile() {
# (SCRIPTSDIR/packages_qubes_minimal.list) # (SCRIPTSDIR/packages_qubes_minimal.list)
exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true exists "${SCRIPTSDIR}/${path_parts[base]}_${template_flavor}${path_parts[dotext]}" || true
fi fi
done
} }
copyTreeExec() { copyTreeExec() {
@ -298,7 +306,9 @@ copyTreeExec() {
local template_flavor="$3" local template_flavor="$3"
local target_dir="$4" local target_dir="$4"
local template_dir="$(templateDir ${template_flavor})" local template_dirs="$(templateDirs ${template_flavor})"
for template_dir in ${template_dirs[@]}; do
local source_dir="$(readlink -m ${source_dir:-${template_dir}}/${dir})" local source_dir="$(readlink -m ${source_dir:-${template_dir}}/${dir})"
local target_dir="$(readlink -m ${target_dir:-${INSTALLDIR}})" local target_dir="$(readlink -m ${target_dir:-${INSTALLDIR}})"
@ -319,6 +329,7 @@ copyTreeExec() {
} }
popd popd
fi fi
done
} }
callTemplateFunction() { callTemplateFunction() {