From 556df2eecdc0f838244a012759da0b76bcfeb2e7 Mon Sep 17 00:00:00 2001 From: lnc3l0t Date: Thu, 1 Aug 2024 18:30:23 +0200 Subject: [PATCH] fix: checking version was too strict (#92) * fix: checking version was too strict check_version now doesn't fail when `tool` has version `0.X` and `threshold` is set to `0.X.0` * fix: checking subversion failed As @LangLangBart suggested a tool with version "1" would pass the check against version "1.0" but not against "1.0.0". It now works for whatever subversion NOTE: code is also cleaner * fix: remove redundant command in version check --------- Co-authored-by: lnk3 Co-authored-by: LangLangBart <92653266+LangLangBart@users.noreply.github.com> --- gh-notify | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gh-notify b/gh-notify index 222257e..bc3049d 100755 --- a/gh-notify +++ b/gh-notify @@ -618,19 +618,20 @@ select_notif() { # This function validates the version of a tool. check_version() { local tool=$1 threshold=$2 on_error=${3:-die} - local user_version + local user_version user_version_part index declare -a ver_parts threshold_parts user_version=$(command $tool --version 2>&1 | - command command grep --color=never --extended-regexp --only-matching --regexp='[0-9]+(\.[0-9]+)*' | + command grep --color=never --extended-regexp --only-matching --regexp='[0-9]+(\.[0-9]+)*' | command sed q) IFS='.' read -ra ver_parts <<<"$user_version" IFS='.' read -ra threshold_parts <<<"$threshold" - for i in "${!threshold_parts[@]}"; do - if ((i >= ${#ver_parts[@]})) || ((ver_parts[i] < threshold_parts[i])); then + for index in "${!threshold_parts[@]}"; do + user_version_part=${ver_parts[index]:-0} + if ((user_version_part < threshold_parts[index])); then $on_error "Your '$tool' version '$user_version' is insufficient. The minimum required version is '$threshold'." - elif ((ver_parts[i] > threshold_parts[i])); then + elif ((user_version_part > threshold_parts[index])); then break fi done