diff --git a/github/check.gitea.update b/github/check.gitea.update index 47dccd5..9807eb2 100755 --- a/github/check.gitea.update +++ b/github/check.gitea.update @@ -1,7 +1,7 @@ #!/bin/sh # Purpose {{{ ## Create a temp file (to monitor) if an upgrade is available for Gitea -## project on Github. +## project on dl.gitea.io/Github. ## It based on gitea's binary available in $PATH or give as first argument. ## How-to use {{{ ### 1. Needs releasetags script, in the same directory @@ -18,76 +18,306 @@ ## }}} # }}} -# Expect max 1 argument {{{ -if [ $# -gt 1 ] -then +# Vars {{{ +readonly PROGNAME=$(basename "${0}") +readonly PROGDIR=$(readlink -m $(dirname "${0}")) +readonly ARGS="${*}" +readonly NBARGS="${#}" +## Test if DEBUG is already defined (by parent script,…) +[ -z "${DEBUG}" ] && DEBUG=1 + +## Default values for some vars +GITEA_BIN_PATH_DEFAULT="$(command -v gitea)" +GITEA_DOWNLOAD_DEFAULT=1 +gitea_download_github=1 + +## Colors +readonly PURPLE='\033[1;35m' +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +readonly COLOR_DEBUG="${PURPLE}" +# }}} + +usage() { # {{{ + cat << HELP -check.gitea.update -- +usage: $PROGNAME (/path/to/gitea) [-b|-p|-d|-h] + Compare current version of an installed Gitea and the last available. -EXAMPLE : +EXAMPLES : - Compare the current version of Gitea with bin available in \$PATH - check.gitea.update + ${PROGNAME} - Compare the current version of Gitea at a specific place - check.gitea.update /srv/bin/gitea + ${PROGNAME} /srv/bin/gitea + ${PROGNAME} --path /srv/bin/gitea + + - Compare version and try to download the new version if require, + next to current bin with '.to.upgrade.vX.YY.ZZ' suffix. + ${PROGNAME} --download + ${PROGNAME} --path /srv/bin/gitea --download + + - Compare version and try to download the new version from github. + ${PROGNAME} --download --github + +OPTIONS : + -b,--bin,-p,--path + Set the Gitea's path to use. + + --download + Try to download the new release if available. + + --github + Download new version from Github repository + (dl.gitea.io is used by default). + + -d,--debug + Enable debug messages. + + -h,--help + Print this help message. HELP - - exit 1 - -fi +} # }}} +debug_message() { # {{{ -# Vars {{{ -DEBUG=1 + local_debug_message="${1}" -script_wd=$(dirname "${0}") + ## Print message if DEBUG is enable (=0) + [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}" -gitea_repo_url="https://github.com/go-gitea/gitea" -gitea_new_version=$("${script_wd}"/releasetags "${gitea_repo_url}" | grep --invert-match --extended-regexp -- '(dev|rc|^$)' | head --lines=1 | sed 's/v//') + unset local_debug_message -gitea_new_version_file="/tmp/.github.gitea.upgrade" + return 0 +} +# }}} +error_message() { # {{{ -if [ $# -eq 1 ] ## If an argument was gave -then - gitea_bin_path="${1}" + local_error_message="${1}" + local_error_code="${2}" - ### Define new version URL and path only if Gitea's absolut path was given - gitea_new_version_url="https://github.com/go-gitea/gitea/releases/download/v${gitea_new_version}/gitea-${gitea_new_version}-linux-amd64" + ## Print message + printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}" + + exit "${local_error_code:=66}" +} +# }}} +is_var_empty() { # {{{ + + ## Return False by default + return_var_empty="1" + ## Total number of variables to test + local_total_var_empty="${#}" + + loop_count_var_empty="0" + + ## While it remains a variable to test + while [ "${local_total_var_empty}" -gt "${loop_count_var_empty}" ]; do + debug_message "is_var_empty − \ +Test var: ${1}." + ### Test if this is empty and set return value to True + [ -z "${1}" ] && return_var_empty="0" + + ### Increase the number of tested variables + loop_count_var_empty=$((loop_count_var_empty+1)) + + ### Shift to the next variable + shift + done + + unset local_total_var_empty + unset loop_count_var_empty + + return "${return_var_empty}" +} +# }}} +is_file_empty() { # {{{ + + local_file="${1}" + + ## File is empty by default + return_is_file_empty="0" + + ### Check if the file is empty + if [ ! -s "${local_file}" ]; then + return_is_file_empty="0" + debug_message "is_file_empty − \ +The file ${RED}${local_file}${COLOR_DEBUG} is empty or doesn't exists." + else + return_is_file_empty="1" + debug_message "is_file_empty − \ +The file ${RED}${local_file}${COLOR_DEBUG} exists and has a size greater than zero." + fi + + return "${return_is_file_empty}" + +} +# }}} +define_vars() { # {{{ + + ## If gitea_bin_path wasn't defined (argument) {{{ + ## Use default value + is_var_empty "${gitea_bin_path}" \ + && gitea_bin_path="${GITEA_BIN_PATH_DEFAULT}" + ## }}} + ## If gitea_download wasn't defined (argument) {{{ + ## Use default value + is_var_empty "${gitea_download}" \ + && gitea_download="${GITEA_DOWNLOAD_DEFAULT}" + ## }}} + + ## Directory where is current Gitea bin + gitea_bin_dir=$(readlink -m $(dirname "${gitea_bin_path}")) + + ## Main Gitea repo's URL + gitea_repo_url="https://github.com/go-gitea/gitea" + + ## Gitea's versions + gitea_current_version=$("${gitea_bin_path}" --version | cut --delimiter=' ' --fields=3) + gitea_new_version=$("${PROGDIR}"/releasetags "${gitea_repo_url}" | grep --invert-match --extended-regexp -- '(dev|rc|^$)' | head --lines=1 | sed 's/v//') + + ## Gitea new version's URL + if [ "${gitea_download_github}" -eq "0" ]; then + gitea_new_version_url="https://github.com/go-gitea/gitea/releases/download/v${gitea_new_version}/gitea-${gitea_new_version}-linux-amd64" + else + gitea_new_version_url="https://dl.gitea.io/gitea/${gitea_new_version}/gitea-${gitea_new_version}-linux-amd64" + fi + ## Gitea new version's path gitea_new_bin_path="${gitea_bin_path}.to.upgrade.v${gitea_new_version}" -else - gitea_bin_path="gitea" - fi - -gitea_current_version=$("${gitea_bin_path}" --version | cut --delimiter=' ' --fields=3) + ## Temp file to monitor + gitea_new_version_file="/tmp/.github.gitea.upgrade" +} # }}} -# Check if the current version is the last one {{{ -if [ "${gitea_current_version}" != "${gitea_new_version}" ]; then - [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Test version — Current version (${gitea_current_version}) and new one (${gitea_new_version}) seems to be different." +main() { # {{{ - ## Create a temp file to monitor - touch -- "${gitea_new_version_file}" - printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Gitea (current : ${gitea_current_version}) : ${gitea_new_version}." >> "${gitea_new_version_file}" + define_vars - ## If the variables were defined - if [ -n "${gitea_new_version_url}" ] && [ -n "${gitea_new_bin_path}" ]; then - ### Download and prepare the new bin {{{ - [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Download Gitea binary from Gitea repository on Github to ${gitea_new_bin_path} ." - wget --quiet "${gitea_new_version_url}" --output-document="${gitea_new_bin_path}" - chmod +x -- "${gitea_new_bin_path}" - ### }}} + # If gitea_bin_path is empty {{{ + # Exit with error + is_var_empty "${gitea_current_version}" \ + && error_message "Can't get current version from Gitea's binary (${gitea_bin_path})." 1 + # }}} + + # If an update is available {{{ + if [ "${gitea_current_version}" != "${gitea_new_version}" ]; then + debug_message "Test version − \ +Current version (${RED}${gitea_current_version}${COLOR_DEBUG}) and new one (${RED}${gitea_new_version}${COLOR_DEBUG}) seems different." + + ## Create a temp file to monitor + touch -- "${gitea_new_version_file}" + printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Gitea (current : ${gitea_current_version}) : ${gitea_new_version}." >> "${gitea_new_version_file}" + + ## If new release should be downloaded + if [ "${gitea_download}" -eq "0" ]; then + ### Download and prepare the new bin {{{ + debug_message "Test version − \ +Download Gitea binary (from ${gitea_new_version_url} ) to ${RED}${gitea_new_bin_path}${COLOR_DEBUG} ." + wget --quiet "${gitea_new_version_url}" --output-document="${gitea_new_bin_path}" + ### }}} + ### If the new binary file is empty {{{ + ### Ensure to remove (don't want an empty upgrade file) + ### Exit with error + is_file_empty "${gitea_new_bin_path}" \ + && rm --force -- "${gitea_new_bin_path}" \ + && error_message "New binary (${gitea_new_bin_path}) is empty or doesn't exists." 2 + ### }}} + chmod +x -- "${gitea_new_bin_path}" + fi + # }}} + + # Gitea is up-to-date {{{ + else + debug_message "Test version − \ +The current version (${RED}${gitea_current_version}${COLOR_DEBUG}) is up-to-date." + + debug_message "Test version − \ +Ensure to remove temp file and any previous and unused releases." + rm --force -- "${gitea_new_version_file}" + find "${gitea_bin_dir}" -type f -iname "gitea.to.upgrade*" -delete + fi + # }}} + +} +# }}} + +# Manage arguments # {{{ +# This code can't be in a function due to arguments + +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + ## If the first argument is not an option + if ! printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+"; + then + ## Consider it as the path to Gitea + gitea_bin_path="${1}" + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) fi + # Parse all options (start with a "-") one by one + while printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+"; do + + case "${1}" in + -b|--bin|-p|--path ) ## Define gitea_bin_path + ## Move to the next argument + shift + ## Define var + readonly gitea_bin_path="${1}" + ;; + --download ) ## Enable download_mode + ## Try to download new release if available + gitea_download=0 + ;; + --github ) ## Download new release from Github + ## Try to download new release from Github + gitea_download_github=0 + ;; + -d|--debug ) ## debug + DEBUG=0 + ;; + -h|--help ) ## help + usage + ## Exit after help informations + exit 0 + ;; + -- ) ## End of options list + ## End the while loop + break + ;; + * ) ## unknow option + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + exit 1 + ;; + esac + + debug_message "Arguments management − \ +${RED}${1}${COLOR_DEBUG} option managed." + + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) + + done + + debug_message "Arguments management − \ +${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed." else - [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Test version — The current version is up-to-date." - rm --force -- "${gitea_new_version_file}" - [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG : Test version — Ensure to remove any previous and unused releases." - find /usr/local/bin -type f -iname "gitea.to.upgrade*" -delete + debug_message "Arguments management − \ +No arguments/options to manage." fi + # }}} +main + exit 0