dotfiles.2022/_home/dot_sz.shrc.d/01_util.functions

65 lines
1.9 KiB
Plaintext
Raw Normal View History

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 "$@"; }
2022-09-29 22:29:13 +00:00
warn() {
(>&2 printf '%s %s\n' $(ANSI_Yellow 'WARNING:') "$(echo "$@")" )
2022-09-29 22:29:13 +00:00
}
# 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
2022-07-15 00:00:09 +00:00
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
2022-07-15 00:00:09 +00:00
}
require_root() {
if [[ $EUID -ne 0 ]]; then
eval $(error 1 "This script should only be run using sudo or as the root user")
2022-07-15 00:00:09 +00:00
fi
}
2022-09-29 22:29:13 +00:00
recommend_root() {
if [[ $EUID -ne 0 ]]; then
warn "This script works better using sudo or as the root user"
return 3
fi
}
2022-07-15 00:00:09 +00:00
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
2022-07-15 00:00:09 +00:00
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}"
}