diff --git a/CentOS/7-Days-To-Die/7D2D-Manage.bash b/CentOS/7-Days-To-Die/7D2D-Manage.bash index 53c2522..520c0a1 100644 --- a/CentOS/7-Days-To-Die/7D2D-Manage.bash +++ b/CentOS/7-Days-To-Die/7D2D-Manage.bash @@ -82,7 +82,8 @@ start_server() { server_session_name="${prefix}-${server_name}" [[ ! -d "${server_directory}" ]] && - log "error" "Unable to find the server directory for $(important "${server_name}")" + log "error" "Unable to find the server directory for $(important "${server_name}")" && + return 1 local server_port @@ -178,7 +179,7 @@ kill_server() { log "info" "Stopped $(important "${prefix}-Server-${server_id}")" return 0 else - log "error" "Could not find $(important "${prefix}-Server-${server_id}") or unable to shut down ${prefix}-Server-${server_id}" + log "error" "$(important "${prefix}-Server-${server_id}") is not running" return 1 fi } @@ -295,6 +296,71 @@ update() { } +list_servers() { + local picked_option + picked_option="" + while :; do + case ${1} in + -h | -\? | --help) + printf "Usage: %s\n" \ +"list [OPTIONS] + --running | -r + Lists the currently running 7 Days To Die Servers + + Example: + --running + --installed | -i + Lists the currently installed 7 Days To Die Servers + + Example: + --installed" + exit + ;; + --) # End of all options. + break + ;; + --running | -r) + picked_option=0 + ;; + --installed | -i) + picked_option=1 + ;; + -?*) + printf 'Unknown option: %s\n' "$1" >&2 + return 1 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift + done + + [[ -z "${picked_option}" ]] && + log "error" "An option must be passed for list, check list -h" && + return 1 + + if (( picked_option == 0 )); then + log "debug" "Listing running servers" + local tmux_sessions + tmux_sessions="$(tmux list-sessions)" >/dev/null 2>&1 + if [[ ! "${?}" -eq "0" ]]; then + important "No servers currently running." + fi + while read -r; do + if [[ "${REPLY}" = *"-Server-"* ]]; then + local running_server + running_server="$(echo "${REPLY}" | cut -d ":" -f1)" + important "${running_server}" + fi + done <<< "${tmux_sessions}" + elif (( picked_option == 1 )); then + log "debug" "Listing installed servers" + while read -r; do + important "${BASE_DIR}/${REPLY}" + done <<< "$(find "${BASE_DIR}" -name "startserver.sh" | cut -d "/" -f5)" + fi +} + usage() { # Print out usage instructions for the local script # @@ -316,7 +382,9 @@ usage() { install Exposes options to install 7 Days To Die Servers, pass -h to it for details update - Exposes options to update 7 Days To Die Servers, pass -h to it for details" + Exposes options to update 7 Days To Die Servers, pass -h to it for details + list + Exposes options to list 7 Days To Die Servers, pass -h to it for details" } @@ -363,6 +431,11 @@ main() { update "$@" break ;; + list | l) + shift + list_servers "$@" + break + ;; -?*) printf "Unknown option: %s\n" "$1" >&2 usage