Skip to content

Commit 036e2ef

Browse files
authored
parse release
1 parent 0ce729c commit 036e2ef

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

kubectl-ktool.sh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ get_latest_release() {
7070
echo "$LATEST_RELEASE"
7171
}
7272

73+
# parse_major_version extracts the major version number from a release string.
74+
#
75+
# Arguments:
76+
# $1: The release string (e.g., "v1.2.3").
77+
#
78+
# Outputs:
79+
# The major version number (e.g., "1").
80+
# Returns empty and a non-zero exit code on failure.
81+
parse_major_version() {
82+
local release_string="$1"
83+
if [[ "$release_string" =~ v([0-9]+) ]]; then
84+
echo "${BASH_REMATCH[1]}"
85+
else
86+
return 1
87+
fi
88+
}
89+
7390
# check_for_updates checks if a newer version of the script is available.
7491
# It performs a quick check in the background and warns the user if an
7592
# update is found. It enforces mandatory updates for new major versions.
@@ -89,12 +106,19 @@ check_for_updates() {
89106
return
90107
fi
91108

92-
# Compare major version numbers (e.g., the '1' in 'ktool-v1.2.3').
93109
local CURRENT_MAJOR_RELEASE
94-
CURRENT_MAJOR_RELEASE=$(echo "$RELEASE" | cut -d'ktool-v' -f2 | cut -d'.' -f1)
110+
CURRENT_MAJOR_RELEASE=$(parse_major_version "$RELEASE")
111+
if [ -z "$CURRENT_MAJOR_RELEASE" ]; then
112+
WARN "Could not parse current release version: ${RELEASE}"
113+
return
114+
fi
95115

96116
local LATEST_MAJOR_RELEASE
97-
LATEST_MAJOR_RELEASE=$(echo "$LATEST_RELEASE" | cut -d'ktool-v' -f2 | cut -d'.' -f1)
117+
LATEST_MAJOR_RELEASE=$(parse_major_version "$LATEST_RELEASE")
118+
if [ -z "$LATEST_MAJOR_RELEASE" ]; then
119+
WARN "Could not parse latest release version: ${LATEST_RELEASE}"
120+
return
121+
fi
98122

99123
if [ "$LATEST_MAJOR_RELEASE" -gt "$CURRENT_MAJOR_RELEASE" ]; then
100124
# For major version changes, the update is considered mandatory for most commands.

0 commit comments

Comments
 (0)