From b842e7c4f67b65500aff91add58219d99796d1b3 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 13 Nov 2021 20:56:19 -0600 Subject: [PATCH] Full blown templating in bash, Trim function, better DEBUG support in Logging-RGB --- .../7-Days-To-Die/7-Days-To-Die-Manage.bash | 284 +++++++++++++++ Misc/Templater.bash | 327 ++++++++++++++++++ Templates/Functions/Logging/Logging-RGB.bash | 10 +- Templates/Functions/Trim.bash | 16 + 4 files changed, 632 insertions(+), 5 deletions(-) create mode 100644 CentOS/7-Days-To-Die/7-Days-To-Die-Manage.bash create mode 100755 Misc/Templater.bash create mode 100755 Templates/Functions/Trim.bash diff --git a/CentOS/7-Days-To-Die/7-Days-To-Die-Manage.bash b/CentOS/7-Days-To-Die/7-Days-To-Die-Manage.bash new file mode 100644 index 0000000..cc8338d --- /dev/null +++ b/CentOS/7-Days-To-Die/7-Days-To-Die-Manage.bash @@ -0,0 +1,284 @@ +### CONSTANTS ### +START_PORT_RANGE=30000 +BASE_DIR="~/7-Days-To-Die" +CONFIG_DIRECTORY="${BASE_DIR}/config" +### CONSTANTS ### + +echo_rgb() { + # Echo a colored string to the terminal based on rgb values + # + # Positional Arguments: + # + # message + # - The message to be printed to stdout + # red + # - The red value from 0 to 255 + # green + # - The green value from 0 to 255 + # blue + # - The blue value from 0 to 255 + # + # Usage: + # echo_rgb "Yep" 10 8 30 + # + # POSIX Compliant: + # N/A + # + + local red + local green + local blue + local input + + input="${1}" + red="${2}" + green="${3}" + blue="${4}" + + printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}" +} + +log() { + # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. + # + # Arguments: + # level + # - The log level, defined within a case check in this function + # message + # - The info message + # line_number + # - The line number of the calling function (${LINNO}) + # + # Usage: + # log "info" "Could not find that directory" + # + # POSIX Compliant: + # Yes + # + + # Set debug status depending if a global debug variable has been set to either 1 or 0 + local debug + if [ ${DEBUG} ]; then + debug=${DEBUG} + else + debug=0 + fi + + local FORMAT + FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]" + + # Convert the level to uppercase + local level + level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') + + local message + message="${2}" + + case "${level}" in + INFO) + # Output all info log levels to stdout + printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1 + return 0 + ;; + WARN | WARNING) + # Output all info log levels to stdout + printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 + return 0 + ;; + DEBUG) + [[ ${debug} == 0 ]] && return + printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 + return 0 + ;; + ERROR) + # Output all error log levels to stderr + printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2 + return 0 + ;; + # Further log levels can be added by extending this switch statement with more comparisons + + *) # Default case, no matches + # Returns non-zero code as an improper log option was passed, this helps with using `set -e` + printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2 + return 1 + ;; + esac +} + +important() { + echo_rgb "${1}" 135 195 255 +} + +confirmation() { + # Receive confirmation from user as y, Y, n, or N + # returns 0 when answer is yes and 1 when answer is no + # + # Arguments: + # message + # - The confirmation prompt sent to the user, for example: + # Would you like to overwrite foobar.txt (y/N)? + # + # Usage: + # confirmation "Some prompt" + # - Sends "Some prompt" to the user and gets their input + # + # POSIX Compliant: + # Yes + # + + local message + message="${1}" + + local choice + + while true; do + read -p "${message} " -n 1 -r choice + case "$choice" in + y | Y) + echo "" + return 0 + ;; + n | N) + echo "" + return 1 + ;; + *) echo -e "\nInput must be either y, Y, n, or N" ;; + esac + done +} + +install_configuration() { + local conf_file + conf_file="${CONFIG_DIRECTORY}/${1}" + + if [[ ! -f "${conf_file}" ]]; then + log "WARNING" "Unable to find the configuration file ${conf_file}" + return 1 + fi +} + +install() { + local server_id + server_id="" + + while :; do + case ${1} in + -h | -\? | --help) + printf "Usage: %s\n" \ + "install [options] + --server | -s + Installs the server to the given id if it doesn't exist + + Example: + --server 3" + exit + ;; + --) # End of all options. + break + ;; + --server | -s) + shift + server_id="${1}" + [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 + ;; + -?*) + printf 'Unknown option: %s\n' "$1" >&2 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift + done + + [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 + + local prefix + + prefix="7D2D" + + local server_directory + local server_config + server_directory="${BASE_DIR}/Server-${server_id}" + server_config="${server_directory}/Mordhau/Saved/Config/LinuxServer/Game.ini" + + [[ -d "${server_directory}" ]] && + log "error" "A server already exists at ${server_directory}, delete it and try again" && + exit 1 + + kill_server -s "${server_id}" >/dev/null 2>&1 + mkdir -p "${server_directory}" && log "info" "Created server directory ${server_directory}" + + steamcmd +login anonymous +force_install_dir "${server_directory}" +app_update 629800 validate +quit + log "info" "Successfully installed Server-${server_id}" + + log "info" "Starting server to install default configuration files, please wait..." + + run_and_stop "${server_id}" + + cat "${server_config}" >"${server_directory}/Mordhau/Saved/Config/LinuxServer/Game-Primary.ini" && + log "info" "Created the Game-Primary.ini file" + + log "info" "Finished setting up Server-${server_id}" +} + +parse_args() { + # Parse input arguments + # + # Arguments: + # Consult the `usage` function + # + # Usage: + # parse_args "$@" + # - All arguments should be ingested by parse_args first for variable setting + # + # POSIX Compliant: + # Yes + # + + while :; do + case ${1} in + -h | -\? | --help) + usage # Display a usage synopsis. + exit + ;; + --) # End of all options. + break + ;; + start | s) + shift + start "$@" + break + ;; + kill | k) + shift + kill_server "$@" + break + ;; + install | i) + shift + install "$@" + break + ;; + update | u) + shift + update "$@" + break + ;; + configure | c) + shift + configure "$@" + break + ;; + -?*) + printf 'Unknown option: %s\n' "$1" >&2 + usage + exit 1 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift + done +} + +parse_args "$@" diff --git a/Misc/Templater.bash b/Misc/Templater.bash new file mode 100755 index 0000000..489d4db --- /dev/null +++ b/Misc/Templater.bash @@ -0,0 +1,327 @@ +#!/usr/bin/env bash + +# "Import" our needed functions from our library +LIBRARY_PATH="/usr/local/lib/Custom-Scripts" +# shellcheck source=/usr/local/lib/ +source "${LIBRARY_PATH}/Logging-RGB.bash" || echo "Unable to source Logging-RGB.bash at ${LIBRARY_PATH}" +# shellcheck source=/usr/local/lib/ +source "${LIBRARY_PATH}/Trim.bash" || log "error" "Unable to source Trim.bash at ${LIBRARY_PATH}" + +# Tmp file used because most distros have this dir exist in RAM, use PID of our process to write to +TEMP_FILE="/tmp/$$" + +# Set the Contents directory to the env variable, script set, or to their .contents file in the user home +CONTENTS_DIRECTORY="${CONTENTS_DIRECTORY:=\ + $(mkdir -p "${HOME}/.contents" && echo "${HOME}/.contents")}" + +EXIT_ON_ERROR=0 + +REPEAT=1 +WRITE_TEMPLATE_MANUALLY_SET=1 +WRITE_TEMPLATE_TO="" + +declare -A template_handlers + +template_handlers=( + ["FILE"]="file_handler" + ["WRITE-TO"]="write_to_handler" + ["TEMPLATE"]="template_handler" +) + +write_content() { + local content + local leading_spaces + content="${1}" + leading_spaces="${2}" + + local string_builder + string_builder="" + + for ((i=1; i<=leading_spaces; i++)); do + string_builder="${string_builder} " + done + string_builder="${string_builder}${content}" + _write_to_buffer "${string_builder}" +} + +_write_to_buffer() { + log "debug" "Received content to write to buffer: +${1}" + printf "%s\n" "${1}" >> "${TEMP_FILE}" + log "debug" "Finished writing content to buffer" +} + +template_handler() { + local template_file + template_file="${1}" + + local leading_spaces + leading_spaces="${2}" + # Check that the given file is not an absolute path + if [[ ! "${template_file:0:1}" == "/" ]]; then + template_file="${CONTENTS_DIRECTORY}/${template_file}" + fi + if [[ ! -f "${template_file}" ]]; then + log "error" "The given template file \"${template_file}\" does not exist" + return 1 + fi + + file_handler "${template_file}" "${leading_spaces}" + REPEAT=0 +} + +file_handler() { + local file + file="${1}" + + local leading_spaces + leading_spaces="${2}" + + # Check that the given file is not an absolute path + if [[ ! "${file:0:1}" == "/" ]] && [[ ! -f "${file}" ]]; then + file="${CONTENTS_DIRECTORY}/${file}" + fi + + + if [[ ! -f "${file}" ]]; then + log "error" "The given file \"${file}\" does not exist" + return 1 + fi + + while IFS="\n" read -r line; do + write_content "${line}" "${leading_spaces}" + done < "${file}" +} + + +write_to_handler() { + local write_path + write_path=${1} + + log "debug" "Received write path \"${write_path}\"" + if [[ ! "${write_path:0:1}" == "/" ]]; then + log "error" "Given write path was invalid, received \"${write_path}\", the path must be an absolute path" + return 1 + fi + + if [[ -d "${write_path}" ]]; then + log "error" "Given write path was invalid, received \"${write_path}\", the path must be a path to an output file (doesn't have to exist), not a directory" + return 1 + fi + # This means that it was NOT manually set, 0 is true in shell + if (("${WRITE_TEMPLATE_MANUALLY_SET}" == 1)); then + WRITE_TEMPLATE_TO=${write_path} + fi +} + +parse_template_extracted() { + local tmpl_extracted + tmpl_extracted="${1}" + + local full_line + full_line="${2}" + + local num_leading_spaces + num_leading_spaces=$(awk -F"[ ]" '{for(i=1;i<=NF && ($i=="");i++);print ""i-1""}' <<< "${full_line}") + + # Basic checks to ensure template is correct, first not empty, second that an '=' was passed to give type specifier + # third is to ensure only one equals sign is passed + if [[ -z "${tmpl_extracted}" ]]; then + log "error" "Template passed was empty... ignoring" + return 1 + elif [[ ! "${tmpl_extracted}" = *"="* ]]; then + log "error" "Template line passed was of an invalid format, missing = specifier, assumed type is missing as well" + return 1 + elif [ "$(echo "${tmpl_extracted}" | grep -o "=" | wc -l)" -gt 1 ]; then + log "error" "Template line passed had too many \"=\", there should only be 1" + return 1 + fi + + local var_type + var_type="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f1)")" + + # Shellcheck incorrectly reads the below line + # shellcheck disable=SC2116 + var_type="$(echo "${var_type^^}")" + + local var_arg + var_arg="$(trim "$(echo "${tmpl_extracted}" | cut -d "=" -f2)")" + + local handler_to_use + handler_to_use="${template_handlers[${var_type}]}" + + if [[ -z "${handler_to_use}" ]]; then + log "error" "Invalid handler found, no such handler \"${var_type}\" exists as a handler" + return 1 + else + log "debug" "Using handler \"${handler_to_use}\"" + if ! eval "${handler_to_use}" "${var_arg}" "${num_leading_spaces}"; then + log "error" "Arguments passed for \"${var_type}\" were invalid, check the argument" + return 1 + fi + log "debug" "Finished using handler \"${handler_to_use}\"" + fi +} + +output_finished_template() { + log "debug" "Outputting finished template" + mkdir -p "$(dirname "${WRITE_TEMPLATE_TO}")" + # We want globbing here + # shellcheck disable=SC2086 + if [[ -z "${WRITE_TEMPLATE_TO}" ]]; then + log "error" "Was never given a path to write the template to, check your template file or explicitly specify an output location" + exit 1 + else + mv "${TEMP_FILE}" ${WRITE_TEMPLATE_TO} + fi +} + +parse_template_line() { + local line + line="${1}" + + if [[ "${line}" = *\{#%*%#\}* ]]; then + tmpl_extracted="${line#*{#%}" + tmpl_extracted="${tmpl_extracted%\%#\}*}" + tmpl_extracted="$(trim "${tmpl_extracted}")" + + log "debug" "Extracted a template: ${tmpl_extracted}" + # Check for non zero status codes and parse the extracted template + if ! parse_template_extracted "${tmpl_extracted}" "${line}"; then + return 1 + fi + log "debug" "Finished extracting template: ${tmpl_extracted}" + else + write_content "${line}" "0" + fi +} + +read_template() { + local read_file + local line + local line_num + line_num=1 + read_file="${1}" + log "info" "Parsing template file \"${read_file}\"" + while IFS="\n" read -r line; do + # Check for non zero status code + if ! parse_template_line "${line}"; then + log "error" "Invalid template passed from \"${read_file}\", check line ${line_num}" + if (( "${EXIT_ON_ERROR}" == 0 )); then + exit 1 + fi + fi + line_num=$((line_num + 1)) + done <"${read_file}" +} + +templater() { + local read_file + read_file="${1}" + [[ ! -f "${read_file}" ]] && + log "error" "$(important "${read_file}") does not exist!" && + return 1 + + read_template "${read_file}" + + if (( "${REPEAT}" == 0 )); then + REPEAT=1 + log "debug" "Repeat set to 0, repeating on the finished file located at ${TEMP_FILE}" + mv "${TEMP_FILE}" "${TEMP_FILE}-repeat" + main "-t" "${TEMP_FILE}-repeat" + else + log "info" "Finished parsing template file \"${read_file}\"" + log "info" "Writing finished template..." + output_finished_template + log "info" "Finished writing template for \"${read_file}\" to \"${WRITE_TEMPLATE_TO}\"" + fi +} + +usage() { + # Print out usage instructions for the local script + # + # Arguments: + # None + # + # Usage: + # usage + # + # POSIX Compliant: + # Yes + # + printf "Usage: %s\n" \ + "$(basename ${0}) -i \"this is some input\" -t \"this is some more example input\" + --input | -i + Example: + --input \"this is an example input\" + --test | -t + Example: + --test \"this is more example input\"" +} + +main() { + # Parse input arguments + # + # Arguments: + # Consult the `usage` function + # + # Usage: + # parse_args "$@" + # - All arguments should be ingested by parse_args first for variable setting + # + # POSIX Compliant: + # Yes + # + local template_file + + while :; do + case ${1} in + -h | -\? | --help) + usage # Display a usage synopsis. + exit + ;; + --) # End of all options. + break + ;; + -t | --template-file) + shift + template_file="${1}" + [[ ! -f "${template_file}" ]] && + log "error" "The given template file \"${template_file}\" does not exist" && + exit 1 + log "info" "Set template file to \"${template_file}\"" + ;; + -c | --contents-dir) + shift + CONTENTS_DIRECTORY="${1}" + [[ ! -d "${CONTENTS_DIRECTORY}" ]] && + log "error" "\"${CONTENTS_DIRECTORY}\" is an invalid path, contents directory must be an absolute path" && + exit 1 + log "info" "Set contents directory to \"${CONTENTS_DIRECTORY}\"" + ;; + -o | --output-to) + shift + WRITE_TEMPLATE_MANUALLY_SET=0 + WRITE_TEMPLATE_TO="${1}" + ;; + -n | --no-exit) + EXIT_ON_ERROR=1 + log "info" "No longer exiting on errors." + ;; + -?*) + printf 'Unknown option: %s\n' "$1" >&2 + usage + exit 1 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift + done + [[ -z "${template_file}" ]] && + log "error" "No template file provided, exiting" && + exit 1 + templater "${template_file}" +} + +main "$@" diff --git a/Templates/Functions/Logging/Logging-RGB.bash b/Templates/Functions/Logging/Logging-RGB.bash index 8cb6069..38a1cd9 100755 --- a/Templates/Functions/Logging/Logging-RGB.bash +++ b/Templates/Functions/Logging/Logging-RGB.bash @@ -1,7 +1,5 @@ #!/bin/bash - - echo_rgb() { # Echo a colored string to the terminal based on rgb values # @@ -79,13 +77,15 @@ log() { return 0 ;; WARN | WARNING) - # Output all info log levels to stdout + # Output all warning log levels to stdout printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 return 0 ;; DEBUG) - [[ ${debug} == 0 ]] && return - printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 + # Output all debug log levels to stdout + if [ "${DEBUG}" ]; then + printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 + fi return 0 ;; ERROR) diff --git a/Templates/Functions/Trim.bash b/Templates/Functions/Trim.bash new file mode 100755 index 0000000..fd4139a --- /dev/null +++ b/Templates/Functions/Trim.bash @@ -0,0 +1,16 @@ +#!/bin/bash + +trim() { + local input="${1}" + + while [[ "${input}" == " "* ]]; do + input="${input## }" + done + + while [[ "${input}" == *" " ]]; do + input="${input%% }" + done + + echo "${input}" +} +