fix(gmod): initConfig should handle multiple occurence

This commit is contained in:
jusito 2024-06-29 14:17:37 +02:00 committed by GitHub
parent d6e98cf13b
commit 236a1fbbb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,28 +11,20 @@ set -o pipefail
CFG_PATH="${SERVER_PATH}/garrysmod/cfg/gmodserver.cfg" CFG_PATH="${SERVER_PATH}/garrysmod/cfg/gmodserver.cfg"
function configReplace() { function configReplace() {
source="$1" local source="$1"
target="\"$2\"" local target="$2"
if ! echo "$2" | grep -qe "^[a-zA-Z0-9_ -]*$"; then count=$(grep -Ece "^\s*${source}\s" "${CFG_PATH}")
echo "[initConfig.sh]Warning your configured value for $source doesn't match regex ^[a-zA-Z0-9_-]+$. Special symbols could crash this script because they are not sanitized."
fi echo "[initConfig.sh]Request for replacing \"$source\" to \"$target\", source is found $count times"
count=$(grep -Poc "($source).+" "${CFG_PATH}" || echo 0) sed -E -i "/^\s${source}\s.*/d" "${CFG_PATH}"
echo "[initConfig.sh]Request for replacing $source to $target, source is found $count times" local file_ends_on_newline=$([ "$(tail -c1 f | wc -l)" -eq 1 ] && echo true || echo false)
if ! $file_ends_on_newline; then
if [ "$count" == "1" ]; then echo "" >> "${CFG_PATH}"
sed -Ei "/${source}.*/d" "${CFG_PATH}" fi
echo "$source $target" >> "${CFG_PATH}" echo "$source \"$target\"" >> "${CFG_PATH}"
elif [ "$count" == "0" ]; then
echo "" >> "${CFG_PATH}"
echo "$source $target" >> "${CFG_PATH}"
else
echo "[initConfig.sh]can't set $1 because there are multiple in"
fi
} }
#create default server.config #create default server.config
@ -56,4 +48,4 @@ if [ -n "${SERVER_RCON_PASSWORD}" ]; then
fi fi
if [ -n "${SERVER_VOICE_ENABLE}" ]; then if [ -n "${SERVER_VOICE_ENABLE}" ]; then
configReplace "sv_voiceenable" "$SERVER_VOICE_ENABLE" configReplace "sv_voiceenable" "$SERVER_VOICE_ENABLE"
fi fi