#! /usr/bin/env bash SCRIPT_DIR=${SCRIPT_DIR:-"$( cd -- "$( dirname -- "$0" )" &> /dev/null && pwd )"} set -e BASE_NAME="${0##*/}" SCRIPT_NAME="${SCRIPT_DIR}/${BASE_NAME}" # Function to compare semantic versions compare_major_version() { local major_version1=$(echo $1 | cut -d. -f1) local major_version2=$(echo $2 | cut -d. -f1) if [[ "$major_version1" == "$major_version2" ]]; then return 0 else return 1 fi } if [[ "${1}" =~ ^(-h|--help$) ]]; then printf '%s\n' \ "Query runtipi's current version and compares with what is available online" \ "" \ "Usage:" \ " ${BASE_NAME} -h | --help | [next] [] " \ "" \ "Arguments:" \ " -h | --help Display usage" \ " version [optional] assume current version - auto-detected when ommitted" \ " next Show the next version (to current-version, see above)" \ "" \ "Exit codes:" \ " 0 (ok) if (next) and on the latest version" \ " if (next) and next version exist and is not a major version change" \ " or when listing versions (not 'next')" \ "" \ " 1 non operational (like --help)" \ " major versions do not match" exit 1 fi # --- tab completion --- completion_bash() { cat < /dev/null && pwd )"} current_version="$1" if [ "$current_version" == "next" ]; then current_version="$2" next_version_only=1 fi if [ -z "$current_version" ]; then [ -r "$runtipi_path/VERSION" ] || runtipi_path=${RUNTIPI_DIR:-"$(cd -- "${SCRIPT_DIR}/../../_" &> /dev/null && pwd )"} current_version=$(cat "$runtipi_path/VERSION") fi # Get the latest release information from GitHub API latest_releases_page=1 latest_releases="" while [ $latest_releases_page -lt ${RELEASE_PAGE_LIMIT:-4} ]; do tmp_releases="$(curl -sL \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/runtipi/runtipi/releases\?page=${latest_releases_page} \ | grep -o '"tag_name": "[^"]*' | cut -d'"' -f4 | grep -E '^v[[:digit:]\.]+$' \ )" || break [ -z "$tmp_releases" ] \ && tmp_releases="EMPTY" && break \ || true latest_releases="$(printf '%s\n' ${latest_releases} ${tmp_releases})" grep -q "^${current_version}$" <<<"${tmp_releases}" \ && tmp_releases="" && break \ || (( latest_releases_page+=1 )) done [ -n "$tmp_releases" ] \ && next_version_only="" && current_version="${current_version} # !not-online!" \ || tmp_releases="" # List everything up to latest version (excluding the latest version) latest_releases="$( <<<"${latest_releases}" sed -n ' 0,/^'"${current_version//\./\\.}"'$/p ' | tac )" next_tag="$( head -n2 <<<"${latest_releases:-${current_version}}" | tail -n1 )" latest_tag="$( tail -n1 <<<"${latest_releases:-${current_version}}")" if [ "$next_version_only" != "1" ]; then printf 'current:\n %-10s\nonline:\n' "$current_version" >&2 [ ${#latest_releases} -eq 0 ] || printf ' %-10s\n' ${latest_releases} >&2 # Compare major version numbers compare_major_version "$latest_tag" "$current_version" elif [ "$next_tag" == "$current_version" ]; then printf "on the latest version\n" #printf "%s\n" "$current_version" >&2 elif [ "$next_tag" != "$current_version" ]; then printf "%s\n" "$next_tag" # Compare major version numbers compare_major_version "$next_tag" "$current_version" fi # vim: set ft=sh expandtab tabstop=4 shiftwidth=4: