You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
1.8 KiB

set -u +e
## Args
# - var - variable name, for example PG_DATA
# - default - default value
default_opt() {
local var="$1"
local default="$2"
local parsed="$(make_opt $var)"
local result="${!var:-$default}"
eval "$parsed=\"$result\""
}
make_opt() {
local name="$1"
echo "_${name,,}"
}
## Args
# - var - variable name, for example PG_DATA
# - error - error message
required_opt() {
local var="$1"
local error="$2"
if [[ ! -z "${!var:-}" ]] ; then
local parsed="$(make_opt $var)"
eval "$parsed=\"\$$var\""
else
echo_exit "$error"
fi
}
## Args
# - path - path to the inode to check
# - uid - desired uid
# - gid - desired gid
check_owner() {
local path="$1"
local desired_uid="$2"
local desired_gid="$3"
uid=$(stat -c '%u' $path || \
echo_exit "Failed to get uid of $path")
gid=$(stat -c '%g' $path || \
echo_exit "Failed to get gid of $path")
[[ $uid == $desired_uid ]] && [[ $gid == $desired_gid ]] || \
echo_exit "Invalid owner for \`$path\`, has $uid:$gid wanted $desired_uid:$desired_gid"
}
## Args
# - path - path to the directory to create
# - uid - desired uid
# - gid - desired gid
mkdir_chown() {
path="$1"
uid="$2"
gid="$3"
if [[ ! -e "$path" ]]
then
mkdir -p "$path"
chown "$uid:$gid" "$path"
else
echo_exit "Path $path already exists"
fi
}
## Args
# - path - path to the directory to create
# - uid - desired uid
# - gid - desired gid
mkdir_chown_permissive() {
path="$1"
uid="$2"
gid="$3"
if [[ ! -e "$path" ]]
then
mkdir -p "$path"
chown "$uid:$gid" "$path"
else
echo "Path $path already exists!"
fi
}
## Args
# - message - message to exit with
echo_exit() {
local msg="$1 Exiting..."
echo $msg
exit 1
}