Tamarin/lib/util.sh

147 lines
2.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2015-08-05 15:49:04 +02:00
HOOKS_DIR="${BASE_DIR}/hooks"
DEFAULT_OPTS_FILE="${BASE_DIR}/tmp/default_opts"
OPT_FILE="${BASE_DIR}/tmp/tamarin/opts"
2015-09-23 21:21:30 +02:00
OPT_PREFIX="tamarin_opt_"
2015-08-04 23:58:43 +02:00
# Colors
COLOR_INFO='\e[0;36m'
COLOR_FATAL='\e[0;31m'
COLOR_WARN='\e[0;33m'
COLOR_SUCCESS='\e[0;32m'
COLOR_ERR='\e[0;37m'
COLOR_OUT='\e[0;37m'
COLOR_DEBUG='\e[0;35m'
function stderr {
if [ -z "$@" ]; then
while read str; do
log ERR "${str}"
done
else
log stderr "$@"
fi
}
function stdout {
if [ -z "$@" ]; then
while read str; do
log OUT "${str}"
done
else
log OUT "$@"
fi
}
function info {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log INFO "${str}"
done
else
log INFO "$@"
fi
}
function warn {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log WARN "${str}"
2015-08-05 15:49:04 +02:00
done
else
log WARN "$@"
2015-08-05 15:49:04 +02:00
fi
}
function debug {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log DEBUG "${str}"
2015-08-05 15:49:04 +02:00
done
else
log DEBUG "$@"
2015-08-05 15:49:04 +02:00
fi
}
2015-07-04 18:45:36 +02:00
function fatal {
2015-08-05 15:49:04 +02:00
if [ -z "$@" ]; then
while read str; do
log FATAL "${str}" >&2
done
else
log FATAL "$@" >&2
fi
exit 1
}
2015-08-04 23:58:43 +02:00
function success {
if [ -z "$@" ]; then
while read str; do
log SUCCESS "${str}"
done
else
log SUCCESS "$@"
fi
}
2015-08-05 15:49:04 +02:00
function log {
local args=( $@ )
local color=COLOR_${args[0]}
echo -e "${!color}[${args[0]}] $(remove_ansi ${args[@]:1})\e[0m"
}
function remove_ansi {
echo "$@" | sed 's,\x1B\[[0-9;]*[a-zA-Z],,g'
2015-08-05 15:49:04 +02:00
}
function load_default_opts {
if [[ -e "${DEFAULT_OPTS_FILE}" ]]; then
info "Loading default opts..."
else
info "No default opts found."
fi
}
2015-08-04 23:58:43 +02:00
function get_opt {
2015-09-23 21:21:30 +02:00
local opt_name=${OPT_PREFIX}${1}
2015-08-05 15:49:04 +02:00
local default_value=${2}
touch "${OPT_FILE}"
source "${OPT_FILE}"
echo ${!opt_name:-${default_value}}
2015-08-04 23:58:43 +02:00
}
function set_opt {
2015-08-05 15:49:04 +02:00
local opt_name=${1}
local opt_value=${2}
mkdir -p "$(dirname ${OPT_FILE})"
2015-08-05 15:49:04 +02:00
touch "${OPT_FILE}"
2015-09-23 21:21:30 +02:00
sed -i "s/^${OPT_PREFIX}${opt_name}*$//" "${OPT_FILE}"
echo "${OPT_PREFIX}${opt_name}=\"${opt_value}\"" >> "${OPT_FILE}"
2015-08-05 15:49:04 +02:00
}
function exec_hooks {
local hook=${1}
local workspace=${2}
local hook_scripts=$( find "${HOOKS_DIR}" -type f -name "*${hook}" -executable | sort )
2015-08-05 15:49:04 +02:00
for hook_script in ${hook_scripts}; do
info "[>> ${hook}] ${hook_script}"
2015-08-05 15:49:04 +02:00
( cd "${workspace}" && "${hook_script}" ) 2> >(stderr) 1> >(stdout)
2015-08-05 15:49:04 +02:00
# 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}"
2015-08-05 15:49:04 +02:00
done
2015-08-04 23:58:43 +02:00
}