docker-minecraft-server/start-utils

155 lines
2.7 KiB
Text
Raw Normal View History

#!/bin/bash
function join_by() {
local d=$1
shift
echo -n "$1"
shift
printf "%s" "${@/#/$d}"
}
function isURL() {
local value=$1
if [[ ${value:0:8} == "https://" || ${value:0:7} == "http://" ]]; then
return 0
else
return 1
fi
}
function isValidFileURL() {
suffix=${1:?Missing required suffix arg}
url=${2:?Missing required url arg}
[[ "$url" == http*://*.${suffix} || "$url" == http*://*.${suffix}\?* ]]
}
function resolveEffectiveUrl() {
url="${1:?Missing required url argument}"
if ! curl -Ls -o /dev/null -w %{url_effective} "$url"; then
log "ERROR failed to resolve effective URL from $url"
exit 2
fi
}
function getFilenameFromUrl() {
url="${1:?Missing required url argument}"
strippedOfQuery="${url%\?*}"
basename "$strippedOfQuery"
}
function isTrue() {
local value=${1,,}
result=
case ${value} in
true | on)
result=0
;;
*)
result=1
;;
esac
return ${result}
}
function isDebugging() {
if [[ -v DEBUG ]] && [[ ${DEBUG^^} == TRUE ]]; then
return 0
else
return 1
fi
}
function debug() {
if isDebugging; then
2020-03-06 15:52:17 +00:00
log "DEBUG: $*"
fi
}
2020-03-06 15:52:17 +00:00
function logn() {
2020-03-06 15:52:17 +00:00
echo -n "[init] $*"
}
function log() {
2020-03-06 15:52:17 +00:00
echo "[init] $*"
}
function logAutopause() {
2020-05-23 14:15:10 +00:00
echo "[Autopause loop] $*"
}
function logAutopauseAction() {
2020-05-23 14:15:10 +00:00
echo "[$(date -Iseconds)] [Autopause] $*"
}
function normalizeMemSize() {
local scale=1
case ${1,,} in
*k)
scale=1024
;;
*m)
scale=1048576
;;
*g)
scale=1073741824
;;
esac
val=${1:0:-1}
echo $((val * scale))
}
function versionLessThan() {
local activeParts
IFS=. read -ra activeParts <<<"${VANILLA_VERSION}"
local givenParts
IFS=. read -ra givenParts <<<"$1"
if ((${#activeParts[@]} < 2)); then
return 1
fi
if ((${#activeParts[@]} == 2)); then
if ((activeParts[0] < givenParts[0])) ||
((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])); then
2020-05-20 13:17:58 +00:00
return 0
else
return 1
fi
else
if ((activeParts[0] < givenParts[0])) ||
((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])) ||
((activeParts[0] == givenParts[0] && activeParts[1] == givenParts[1] && activeParts[2] < givenParts[2])); then
2020-05-20 13:17:58 +00:00
return 0
else
return 1
fi
fi
2020-06-19 16:31:56 +00:00
}
requireVar() {
if [ ! -v $1 ]; then
log "ERROR: $1 is required to be set"
exit 1
fi
if [ -z "${!1}" ]; then
log "ERROR: $1 is required to be set"
exit 1
fi
2020-06-19 16:31:56 +00:00
}
2020-07-26 18:19:45 +00:00
function writeEula() {
if ! echo "# Generated via Docker on $(date)
2020-07-26 18:19:45 +00:00
eula=${EULA,,}
" >/data/eula.txt; then
log "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}"
exit 2
fi
2020-07-26 18:19:45 +00:00
}