truestuff/01_util.functions

65 lines
1.9 KiB
Plaintext

ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; }
ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; }
ANSI_Green() { ANSI_Code=32 ANSI_Print "$@"; }
ANSI_Yellow() { ANSI_Code=33 ANSI_Print "$@"; }
ANSI_Blue() { ANSI_Code=34 ANSI_Print "$@"; }
ANSI_Indiego() { ANSI_Code=35 ANSI_Print "$@"; }
ANSI_Cyan() { ANSI_Code=36 ANSI_Print "$@"; }
warn() {
(>&2 printf '%s %s\n' $(ANSI_Yellow 'WARNING:') "$(echo "$@")" )
}
# The way to call this is:
# eval $(error 1 "message") || exit $?
# where 1 is the return/exit code and "message" is the message to be
# displayed
error() {
printf "(>&2 \
printf '%%s %%b' '$(ANSI_Red 'ERROR:')' '$(printf "%b " "${@:2}")') \
&& echo '' && return ${1} 2>/dev/null || exit ${1}"
}
require_sudo() {
if [[ $EUID -ne 0 && -z $(printf "%s\n" $(id --groups --name) | grep '^sudo$') ]]; then
eval $(error 1 "This script should be run by a sudo memeber or as the root user")
fi
}
require_root() {
if [[ $EUID -ne 0 ]]; then
eval $(error 1 "This script should only be run using sudo or as the root user")
fi
}
recommend_root() {
if [[ $EUID -ne 0 ]]; then
warn "This script works better using sudo or as the root user"
return 3
fi
}
function setacl() {
if [[ $# -lt 4 ]]; then
eval $(error 1 \
"setacl missing arguments, called with '$1' ${@:2}" \
$(printf "%s\n" \
"Usage:" \
"\tsetacl <Options> <Ownership> <Permissions> <Path> [<Additional Paths>...]" \
)
)
return 1
fi
local Options=${1}
local Ownership=${2}
local Permissions=${3}
[[ -n "${SZ_DEBUG}" ]] \
&& echo "chown $Options $Ownership \"${@:4}\""
chown $Options $Ownership "${@:4}"
[[ -n "${SZ_DEBUG}" ]] \
&& echo "chmod $Options $Permissions \"${@:4}\""
chmod $Options $Permissions "${@:4}"
}