fix(gmod): initConfig should handle multiple occurence
This commit is contained in:
		@@ -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
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	count=$(grep -Poc "($source).+" "${CFG_PATH}" || echo 0)
 | 
					    echo "[initConfig.sh]Request for replacing \"$source\" to \"$target\", source is found $count times"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	echo "[initConfig.sh]Request for replacing $source to $target, source is found $count times"
 | 
					    sed -E -i "/^\s${source}\s.*/d" "${CFG_PATH}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if [ "$count" == "1" ]; then
 | 
					    local file_ends_on_newline=$([ "$(tail -c1 f | wc -l)" -eq 1 ] && echo true || echo false)
 | 
				
			||||||
		sed -Ei "/${source}.*/d" "${CFG_PATH}"
 | 
					    if ! $file_ends_on_newline; then
 | 
				
			||||||
		echo "$source $target" >> "${CFG_PATH}"
 | 
					        echo "" >> "${CFG_PATH}"
 | 
				
			||||||
		
 | 
					    fi
 | 
				
			||||||
	elif [ "$count" == "0" ]; then
 | 
					    echo "$source \"$target\"" >> "${CFG_PATH}"
 | 
				
			||||||
		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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user