From a29cf0a83f46ee26b5ec01997b6483199a4785d5 2020-07-04 22:16:24 From: Branko Majic <branko@majic.rs> Date: 2020-07-04 22:16:24 Subject: [PATCH] Noticket: Make Factorio Manager more robust by requesting all variables to be defined before use: - Added missing server setting for port. --- diff --git a/games/factorio_manager.sh b/games/factorio_manager.sh index 6a0e964a6ea5889c2e632a651f32cb0518a6b9f5..d3e263d19ae72fcef1b76165b3bbdf98eff98314 100755 --- a/games/factorio_manager.sh +++ b/games/factorio_manager.sh @@ -18,6 +18,9 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +# Treat unset variables as errors. +set -u + program="factorio_manager.sh" version="0.1" @@ -260,7 +263,7 @@ function colorecho() { color="$1" text="$2" - declare -A colors + declare -A colors=() colors[black]="${_text_black}" colors[red]="${_text_red}" @@ -285,7 +288,7 @@ function colorprintf() { local format="$2" shift 2 - declare -A colors + declare -A colors=() colors[black]="${_text_black}" colors[red]="${_text_red}" @@ -328,7 +331,7 @@ function bold_blue() { # escaped for use in a JSON file. # # Defaults are mostly identical to the ones listed under Factorio's -# default "server-setting.json" with some exceptions to options that +# default "server-settings.json" with some exceptions to options that # may be considered invasion of privacy: # # - User verification is disabled. Otherwise the central @@ -336,6 +339,10 @@ function bold_blue() { # game at the moment. # - Game is specifically configured to be non-public. # +# In addition to settings from the "server-settings.json" +# configuiration file, settings also cover the server port (specified +# in the "config.ini"). +# # Arguments: # # $1 (server_name) @@ -355,16 +362,16 @@ function read_server_settings() { # Local helper variables. local key="" value="" prompt="" confirmed="" item="" validation_passed possible_values i - declare -A settings_prompt - declare -A settings_description - declare -A settings_type - declare -A option + declare -A settings_prompt=() + declare -A settings_description=() + declare -A settings_type=() + declare -A option=() - declare -a settings_order - declare -a possible_options + declare -a settings_order=() + declare -a possible_options=() # Global variables set by the function. - declare -g -A settings_value + declare -g -A settings_value=() # Set-up listings of server settings. Each setting is described # with name, prompt, description, default value, and @@ -394,6 +401,15 @@ function read_server_settings() { settings_type["$key"]="list" settings_order+=("$key") + # Not part of "server-settings.json", but important to show and + # prompt the user for. + key="port" + settings_prompt["$key"]="Port" + settings_description["$key"]="Port on which the server should listen" + settings_value["$key"]="34197" + settings_type["$key"]="int" + settings_order+=("$key") + key="max_players" settings_prompt["$key"]="Maximum players" settings_description["$key"]="Maximum number of players allowed, admins can join even a full server. 0 means unlimited." @@ -703,7 +719,7 @@ manager_directory="$HOME/.factorio" game_installations_directory="$manager_directory/.game_installations" # If no arguments were given, just show usage help. -if [[ -z $1 ]]; then +if [[ -z ${1-} ]]; then short_usage exit "$SUCCESS" fi @@ -739,7 +755,7 @@ shift if [[ $command == set-game-dir ]]; then # Read and verify additional positional arguments. - game_installations_directory="$1" + game_installations_directory="${1-}" shift if [[ -z $game_installations_directory ]]; then @@ -841,7 +857,7 @@ elif [[ $command == create ]]; then fi # Read positional arguments. - instance="$1" + instance="${1-}" shift # Calculate derived variables. @@ -933,7 +949,7 @@ EOF elif [[ $command == launch ]]; then # Read positional arguments. - instance="$1" + instance="${1-}" shift # Set-up derived variables. @@ -1000,8 +1016,8 @@ elif [[ $command == launch ]]; then elif [[ $command == backup ]]; then # Read positional arguments. - instance="$1" - description="$2" + instance="${1-}" + description="${2-}" shift 2 # Use timestamp-based names for backups. @@ -1079,7 +1095,7 @@ elif [[ $command == backup ]]; then elif [[ $command == list-backups ]]; then # Read positional arguments. - instance="$1" + instance="${1-}" shift # Set-up derived variables. @@ -1154,8 +1170,8 @@ elif [[ $command == list-backups ]]; then elif [[ $command == restore ]]; then # Read positional arguments. - instance="$1" - backup_name="$2" + instance="${1-}" + backup_name="${2-}" shift 2 # Set-up derived values. @@ -1283,8 +1299,8 @@ elif [[ $command == restore ]]; then elif [[ $command == remove-backup ]]; then # Read positional arguments. - instance="$1" - backup_name="$2" + instance="${1-}" + backup_name="${2-}" shift 2 # Verify positional arguments. @@ -1367,7 +1383,7 @@ elif [[ $command == remove-backup ]]; then #=============# elif [[ $command == set-version ]]; then # Read positional arguments. - instance="$1" + instance="${1-}" shift # Verify positional arguments. @@ -1465,7 +1481,7 @@ elif [[ $command == info ]]; then fi # Read positional arguments. - instance="$1" + instance="${1-}" shift # Verify positional arguments. @@ -1611,7 +1627,7 @@ elif [[ $command == info ]]; then elif [[ $command == remove ]]; then # Read positional arguments. - instance="$1" + instance="${1-}" shift # Verify positional arguments. @@ -1735,8 +1751,8 @@ elif [[ $command == copy ]]; then fi # Read positional arguments. - source_instance="$1" - destination_instance="$2" + source_instance="${1-}" + destination_instance="${2-}" shift 2 # Verify positional arguments. @@ -1785,7 +1801,7 @@ elif [[ $command == copy ]]; then # be able to detect the game is running in this way. flock --exclusive --nonblock 200 if [[ $? != 0 ]]; then - error "Could not lock instance directory via lock file $lock_file. Is Factorio instance still running?" + error "Could not lock instance directory via lock file $source_lock_file. Is Factorio instance still running?" exit "$ERROR_GENERAL" fi @@ -1888,8 +1904,8 @@ elif [[ $command == import ]]; then fi # Read positional arguments. - instance="$1" - source_directory="$2" + instance="${1-}" + source_directory="${2-}" shift 2 # Verify positional arguments. @@ -1912,7 +1928,7 @@ elif [[ $command == import ]]; then source_lock_file="$source_directory/.lock" # List of entries to import from the source directory. - declare -A import_entries + declare -A import_entries=() import_entries["achievements-modded.dat"]="contains achivement information for modded plays" import_entries["achievements.dat"]="contains achievement information for vanilla plays" import_entries["archive"]="contains desync reports" @@ -1985,7 +2001,7 @@ elif [[ $command == import ]]; then # Copy files and directories. echo - declare -a missing_import_entries + declare -a missing_import_entries=() for source_entry in "${import_entries_keys[@]}"; do source_entry_path="$source_directory/$source_entry" if [[ -e $source_entry_path ]]; then @@ -2033,7 +2049,7 @@ EOF # create-server # #===============# elif [[ $command == create-server ]]; then - instance="$1" + instance="${1-}" # Make sure user has set directory with game installations - test # both symlink and target destination. @@ -2048,7 +2064,7 @@ elif [[ $command == create-server ]]; then fi # Read positional arguments. - instance="$1" + instance="${1-}" shift # Calculate derived variables. @@ -2116,7 +2132,7 @@ locale= [other] check-updates=false enable-crash-log-uploading=false -port=$server_port +port=${settings_value[port]} [interface]