summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel M. Capella <polyzen@archlinux.org>2023-09-09 20:09:32 +0000
committerDaniel M. Capella <polyzen@archlinux.org>2023-09-09 20:09:32 +0000
commita50efef11ee19344c74880f0cea626aa6af3c666 (patch)
tree0550e577224186c73599079b6267dfa17b390eea
parentb5c3555455cc45dfee33804e8e8b118f7595febb (diff)
parentd361c598ce454d79c4b3f9aafee8a80688cbe384 (diff)
Merge branch 'rankmirrors-add-working-only-option' into 'master'
rankmirrors: Add --working-only option to only output working mirrors See merge request pacman/pacman-contrib!37
-rw-r--r--CHANGES.md4
-rw-r--r--src/rankmirrors.sh.in15
2 files changed, 14 insertions, 5 deletions
diff --git a/CHANGES.md b/CHANGES.md
index ee1d151..8af8ba6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
+### Added
+
+- rankmirrors: Add --working-only option to only output working mirrors (!37) (Victor Westerhuis)
+
### Fixed
- Create pipelines for and satisfy markdownlint-cli2, ShellCheck, and Vint (!32, !33, !34, 41554d65, d674bf23) (Matthew Armand, Robin Candau, Daniel M. Capella)
diff --git a/src/rankmirrors.sh.in b/src/rankmirrors.sh.in
index 29197ad..690674c 100644
--- a/src/rankmirrors.sh.in
+++ b/src/rankmirrors.sh.in
@@ -44,6 +44,7 @@ Options:
-t, --times only output mirrors and their response times
-u, --url test a specific URL
-v, --verbose be verbose in output
+ -w, --working-only only output mirrors that respond within the timeout
-h, --help display this help message and exit
-V, --version display version information and exit
EOF
@@ -70,9 +71,10 @@ err() {
# returns the fetching time, or timeout, or unreachable
gettime() {
IFS=' ' read -ra output <<< "$(curl -s -m "$MAX_TIME" -w "%{time_total} %{http_code}" "$1" -o/dev/null)"
- (( $? == 28 )) && echo timeout && return
- (( output[1] >= 400 || ! output[1] )) && echo unreachable && return
+ (( $? == 28 )) && { echo timeout; return 1; }
+ (( output[1] >= 400 || ! output[1] )) && { echo unreachable; return 1; }
echo "${output[0]}"
+ return 0
}
# getfetchurl serverurl (e.g. getturl http://foo.com/core/os/i686)
@@ -158,6 +160,7 @@ while [[ $1 ]]; do
URL="$2";
shift 2;;
verbose) VERBOSE=1 ; shift ;;
+ working-only) WORKING_ONLY=1 ; shift ;;
help) usage ;;
version) version ;;
*) err "'$1' is an invalid argument."
@@ -195,6 +198,7 @@ while [[ $1 ]]; do
URL="$2";
snum=2;;
v) VERBOSE=1 ;;
+ w) WORKING_ONLY=1 ;;
h) usage ;;
V) version ;;
*) err "'$1' is an invalid argument." ;;
@@ -242,8 +246,9 @@ get_url_time() {
server=$1
url="$(getfetchurl "$server")"
[[ $url = fail ]] && err "URL '$server' is malformed."
- time=$(gettime "$url")
- echo "$time $server" >>"$tmpfile"
+ if time=$(gettime "$url") || ! [[ $WORKING_ONLY ]]; then
+ echo "$time $server" >>"$tmpfile"
+ fi
# Output
if [[ $VERBOSE && $TIMESONLY ]]; then
@@ -259,7 +264,7 @@ tmpfile=$(mktemp)
if [[ $PARALLEL ]]; then
servers=()
# Exports for GNU parallel
- export MAX_TIME ARCH TARGETREPO VERBOSE TIMESONLY tmpfile
+ export MAX_TIME ARCH TARGETREPO VERBOSE TIMESONLY WORKING_ONLY tmpfile
export -f getfetchurl gettime get_url_time
fi
for line in "${linearray[@]}"; do