Skip to content

Commit 0a56401

Browse files
author
Sergio Durigan Junior
committed
Improve wcurl version comparison mechanism
We've been having a some problems with version comparison in the script. The approach we've taken so far is to decompose the current curl version into two variables (major and minor), and then perform comparisons against these components separately. It works, but it's confusing. Another possible approach would be to use a C-style version normalization (basically a printf "%02d%02d") and then perform comparisons against the versions we want. The problem is that these versions need also to be normalized, which can be confusing as well. I decided to implement the second approach but abstract it as a simple function that can take a regular version string like "8.16" as well as a comparison operator that will then be passed onto to "test". This reads much nicer and abstracts the complexities of version normalization away. Unfortunately due to the limitations of shell scripting it's not easy to deduplicate code in this scenario, but that should be OK for now. Signed-off-by: Sergio Durigan Junior <[email protected]>
1 parent df07f13 commit 0a56401

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

wcurl

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,39 @@ readonly UNSAFE_PERCENT_ENCODE="%2F %5C"
123123
# Whether to invoke curl or not.
124124
DRY_RUN="false"
125125

126+
# The current version of curl.
127+
CURL_VERSION=""
128+
# The major curl version.
129+
CURL_MAJOR_VERSION=""
130+
# The minor curl version.
131+
CURL_MINOR_VERSION=""
132+
# The normalized curl version.
133+
CURL_NORMALIZED_VERSION=""
134+
126135
# Sanitize parameters.
127136
sanitize()
128137
{
129138
if [ -z "${URLS}" ]; then
130139
error "You must provide at least one URL to download."
131140
fi
132141

133-
readonly CURL_OPTIONS URLS DRY_RUN HAS_USER_SET_OUTPUT
142+
CURL_VERSION=$(curl --version | cut -f2 -d' ' | head -n1)
143+
if [ -z "${CURL_VERSION}" ]; then
144+
error "Unable to determine curl version. Is curl installed?"
145+
fi
146+
147+
CURL_MAJOR_VERSION=$(printf "%s" "${CURL_VERSION}" | cut -f1 -d.)
148+
CURL_MINOR_VERSION=$(printf "%s" "${CURL_VERSION}" | cut -f2 -d.)
149+
CURL_NORMALIZED_VERSION=$(printf "%02d%02d" "${CURL_MAJOR_VERSION}" "${CURL_MINOR_VERSION}")
150+
151+
readonly CURL_OPTIONS \
152+
URLS \
153+
DRY_RUN \
154+
HAS_USER_SET_OUTPUT \
155+
CURL_VERSION \
156+
CURL_MAJOR_VERSION \
157+
CURL_MINOR_VERSION \
158+
CURL_NORMALIZED_VERSION
134159
}
135160

136161
# Indicate via exit code whether the string given in the first parameter
@@ -198,36 +223,40 @@ get_url_filename()
198223
# No slash means there was just a hostname and no path; return empty string.
199224
}
200225

226+
# Given a version (in the format MAJOR.MINOR) as the first argument
227+
# and an operator as the second argument, perform a comparison against
228+
# the current curl version.
229+
compare_curl_version()
230+
{
231+
# Any of: -lt, -le, -eq, -gt, -ge
232+
operator="${1}"
233+
# We only compare the first two digits of the version.
234+
vermaj=$(printf "%s" "${2}" | cut -d. -f1)
235+
vermin=$(printf "%s" "${2}" | cut -d. -f2)
236+
version_to_compare=$(printf "%02d%02d" "${vermaj}" "${vermin}")
237+
238+
test "${CURL_NORMALIZED_VERSION}" "${operator}" "${version_to_compare}"
239+
}
240+
201241
# Execute curl with the list of URLs provided by the user.
202242
exec_curl()
203243
{
204244
CMD="curl "
205245

206-
# Store version to check if it supports --no-clobber, --parallel and --parallel-max-host.
207-
curl_version=$($CMD --version | cut -f2 -d' ' | head -n1)
208-
curl_version_major=$(echo "$curl_version" | cut -f1 -d.)
209-
curl_version_minor=$(echo "$curl_version" | cut -f2 -d.)
210-
211246
CURL_NO_CLOBBER=""
212247
CURL_PARALLEL=""
213248

214-
if [ "${curl_version_major}" -ge 8 ]; then
249+
# --parallel is only supported since 7.66.0.
250+
if compare_curl_version -ge "7.66"; then
251+
CURL_PARALLEL="--parallel"
252+
fi
253+
# --no-clobber is only supported since 7.83.0.
254+
if compare_curl_version -ge "7.83"; then
215255
CURL_NO_CLOBBER="--no-clobber"
216-
CURL_PARALLEL="--parallel --parallel-max-host 5"
217-
218-
# --parallel-max-host is only supported since 8.16.0.
219-
if [ "${curl_version_major}" -eq 8 ] && [ "${curl_version_minor}" -lt 16 ]; then
220-
CURL_PARALLEL="--parallel"
221-
fi
222-
elif [ "${curl_version_major}" -eq 7 ]; then
223-
# --no-clobber is only supported since 7.83.0.
224-
if [ "${curl_version_minor}" -ge 83 ]; then
225-
CURL_NO_CLOBBER="--no-clobber"
226-
fi
227-
# --parallel is only supported since 7.66.0.
228-
if [ "${curl_version_minor}" -ge 66 ]; then
229-
CURL_PARALLEL="--parallel"
230-
fi
256+
fi
257+
# --parallel-max-host is only supported since 8.16.0.
258+
if compare_curl_version -ge "8.16"; then
259+
CURL_PARALLEL="${CURL_PARALLEL} --parallel-max-host 5"
231260
fi
232261

233262
# Detecting whether we need --parallel. It is easier to rely on

0 commit comments

Comments
 (0)