summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2022-11-21 12:19:17 +0100
committerNatanael Copa <ncopa@alpinelinux.org>2022-11-21 12:31:15 +0100
commit6a6db15345350a526c06d3bdb31e395a87755a07 (patch)
tree8d3ea0d2115afddc6beb1bb3d8be2dbed6b41235
parent9dac5805908aa828d949fc0ffca03bb0cb7e9afb (diff)
downloadalpine-conf-6a6db15345350a526c06d3bdb31e395a87755a07.zip
setup-apkrepos: fix mirror counting
when piping the mirror list to `more` the mirror_count is set in a subshell and will end up unset. fixes error when selecting random mirror: /sbin/setup-apkrepos: line 31: arithmetic syntax error fixes commit 860b7271232a (APK Repo Setup)
-rw-r--r--setup-apkrepos.in17
-rwxr-xr-xtests/setup_apkrepos_test10
2 files changed, 18 insertions, 9 deletions
diff --git a/setup-apkrepos.in b/setup-apkrepos.in
index bb60132..5c6d49b 100644
--- a/setup-apkrepos.in
+++ b/setup-apkrepos.in
@@ -16,6 +16,11 @@ get_hostname_from_url() {
echo ${n%%/*}
}
+get_mirror_count() {
+ set -- $MIRRORS
+ echo $#
+}
+
ask_setup_method() {
cat <<-__EOF__
r) Add random from the above list
@@ -23,12 +28,12 @@ ask_setup_method() {
e) Edit ${ROOT}etc/apk/repositories with text editor
__EOF__
- ask "Enter mirror number (1-$mirror_count) or URL to add (or r/f/e/done)" "$1"
+ ask "Enter mirror number (1-$(get_mirror_count)) or URL to add (or r/f/e/done)" "$1"
}
add_random_mirror() {
local i=0
- local random_mirror_index="$(( $RANDOM % $mirror_count ))"
+ local random_mirror_index="$(( $RANDOM % $(get_mirror_count) ))"
printf %s "Picking random mirror... "
for mirror in $MIRRORS; do
@@ -71,10 +76,9 @@ add_fastest_mirror() {
add_mirror "$fastest"
}
-# show mirrors and store how many in global mirror_count
+# show mirrors
show_mirror_list() {
local mirror i=0
- mirror_count=0
[ -z "$MIRRORS" ] && return
echo ""
echo "Available mirrors:"
@@ -83,12 +87,11 @@ show_mirror_list() {
echo "$i) $(get_hostname_from_url $mirror)"
done
echo ""
- mirror_count=$i
}
add_from_list() {
local mirror_index="$1"
- if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $mirror_count ]; then
+ if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $(get_mirror_count) ]; then
return 1
fi
set $MIRRORS
@@ -196,12 +199,10 @@ if $add_first; then
fi
if $add_random; then
- show_mirror_list > /dev/null
add_random_mirror && changed=true
fi
if $add_fastest; then
- show_mirror_list > /dev/null
add_fastest_mirror && changed=true
fi
diff --git a/tests/setup_apkrepos_test b/tests/setup_apkrepos_test
index bf57b4f..bb152c0 100755
--- a/tests/setup_apkrepos_test
+++ b/tests/setup_apkrepos_test
@@ -3,7 +3,8 @@
. $(atf_get_srcdir)/test_env.sh
init_tests \
setup_apkrepos_usage \
- setup_apkrepos_https
+ setup_apkrepos_https \
+ setup_apkrepos_random
setup_apkrepos_usage_body() {
test_usage setup-apkrepos
@@ -20,3 +21,10 @@ setup_apkrepos_https_body() {
grep -q 'https://example.com' etc/apk/repositories || atf_fail "example.com was not added to /etc/apk/repositories"
}
+setup_apkrepos_random_body() {
+ init_env
+ export WGETCONTENT="$(seq 0 9 | awk '{print "https://a" $0 ".example.com"}')"
+ atf_check -s exit:0 \
+ -o match:"Added mirror a[0-9].example.com" \
+ setup-apkrepos -r
+}