osi-tools/lib/osi.sh

74 lines
1.4 KiB
Bash
Raw Normal View History

2018-08-11 18:57:24 +00:00
#!/hint/bash
2018-08-18 18:42:42 +00:00
# Copyright (C) 2018 Luke Shumaker
# Copyright (C) 2023 Umorpha Systems
2018-08-18 18:42:42 +00:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2018-08-11 18:57:24 +00:00
# shellcheck disable=SC2034
readonly EXIT_SUCCESS=0
readonly EXIT_FAILURE=1
readonly EXIT_INVALIDARGUMENT=2
readonly EXIT_NOTIMPLEMENTED=3
readonly EXIT_NOPERMISSION=4
readonly EXIT_NOTINSTALLED=5
readonly EXIT_NOTCONFIGURED=6
readonly EXIT_NOTRUNNING=7
# `gprintf` is the same as `printf`, but feeds the format string
# through `gettext`. This is pretty much just a kludge to silence
# spurious SC2059 complaints without disabling it entirely or leaving
# "shellcheck disable=" comments everywhere.
gprintf() {
if [[ "$1" == '-v' ]]; then
set -- "$1" "$2" "$(gettext -- "$3")" "${@:4}"
else
set -- "$(gettext -- "$1")" "${@:2}"
fi
# shellcheck disable=SC2059
printf "$@"
}
2018-08-11 18:57:24 +00:00
print() {
# shellcheck disable=SC2059
printf "$(gettext -- "$1")\\n" "${@:2}"
}
error() {
local msg
gprintf -v msg "${@:2}"
2018-08-11 18:57:24 +00:00
printf '%s: %s\n' "${0##*/}" "$msg" >&2
if (( $1 != 0 )); then
exit "$1"
fi
}
in_array() {
local needle=$1
local straw
for straw in "${@:2}"; do
if [[ $needle = "$straw" ]]; then
return 0
fi
done
return 1
}
2018-08-11 21:15:13 +00:00
is_sudo() {
(( UID == 0 && ${SUDO_UID:-0} != 0 ))
}
is_root() {
2018-08-11 21:38:08 +00:00
(( UID == 0 ))
2018-08-11 21:15:13 +00:00
}
2018-08-11 18:57:24 +00:00
needs_sudo() {
2018-08-11 21:15:13 +00:00
if ! is_sudo; then
error $EXIT_NOPERMISSION "Must be invoked through sudo."
2018-08-11 18:57:24 +00:00
fi
}
needs_root() {
2018-08-11 21:15:13 +00:00
if ! is_root; then
error $EXIT_NOPERMISSION "Must be invoked as root."
2018-08-11 18:57:24 +00:00
fi
}