Tamarin/lib/util.sh

92 lines
1.6 KiB
Bash

#!/usr/bin/env bash
HOOKS_DIR="${BASE_DIR}/hooks"
OPT_FILE="${BASE_DIR}/tmp/.tamarin_opts"
OPT_PREFIX="tamarin_opt_"
function info {
if [ -z "$@" ]; then
while read str; do
log INFO "${str}"
done
else
log INFO "$@"
fi
}
function debug {
if [ -z "$@" ]; then
while read str; do
log DEBUG "${str}"
done
else
log DEBUG "$@"
fi
}
function error {
if [ -z "$@" ]; then
while read str; do
log ERROR "${str}" >&2
done
else
log ERROR "$@" >&2
fi
}
function fatal {
if [ -z "$@" ]; then
while read str; do
log FATAL "${str}" >&2
done
else
log FATAL "$@" >&2
fi
exit 1
}
function log {
local args=( $@ )
echo "[${HOSTNAME}] [${args[0]}] ${args[@]:1}"
}
function get_opt {
local opt_name=${OPT_PREFIX}${1}
local default_value=${2}
touch "${OPT_FILE}"
source "${OPT_FILE}"
echo ${!opt_name:-${default_value}}
}
function set_opt {
local opt_name=${1}
local opt_value=${2}
touch "${OPT_FILE}"
sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}"
echo "${OPT_PREFIX}${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}"
}
function exec_hooks {
local hook=${1}
local workspace=${2}
local hook_scripts=$( find "${HOOKS_DIR}" -type f -name "*${hook}" -executable)
for hook_script in ${hook_scripts}; do
info "[${hook}] Executing ${hook_script}"
( cd "${workspace}" && "${hook_script}" ) 2> >(error) 1> >(info)
# If the script did not execute properly, we stop here
if [ $? != 0 ]; then
fatal "The '${hook_script}' hook script did not finished properly !"
fi
info "[${hook}] ${hook_script} Done."
done
}