Commit 096c3ce5 authored by Vitaly Lipatov's avatar Vitaly Lipatov

eget: implement --check-mirrors

parent 623ad293
......@@ -153,31 +153,53 @@ __wget()
docmd $WGET $WGETQ $WGETNOSSLCHECK "$@"
fi
}
# put remote content to stdout
scat()
{
__wget -O- "$1"
local URL="$1"
local res
__wget -O- "$URL" && return
res=$?
# TODO: move check mirrors to __wget (__curl) command (check via try download and print progress)
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__wget -O- "$URL"
}
# download to default name of to $2
sget()
{
local URL="$1"
local res
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
scat "$1"
scat "$URL"
return
elif [ -n "$2" ] ; then
__wget -O "$2" "$1"
else
__wget -O "$2" "$URL" && return
res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__wget -O "$2" "$URL"
return
fi
# TODO: поддержка rsync для известных хостов?
# Не качать, если одинаковый размер и дата
# -nc
# TODO: overwrite always
__wget $WGETNAMEOPTIONS "$1"
fi
__wget $WGETNAMEOPTIONS "$URL" && return
res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__wget $WGETNAMEOPTIONS "$URL"
}
check_url_http()
check_url_is_accessible()
{
local URL="$1"
__wget --spider -S "$URL" 2>&1 | grep "HTTP" | tail -n1 | grep -q -w "200\|301"
__wget --spider -S "$URL" 2>&1 | grep "HTTP/" | tail -n1 | grep -q "200"
}
else
......@@ -194,28 +216,72 @@ __curl()
# put remote content to stdout
scat()
{
__curl "$1"
local URL="$1"
local res
__curl "$URL" && return
res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__curl "$URL"
}
# download to default name of to $2
sget()
{
local URL="$1"
local res
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
scat "$1"
return
elif [ -n "$2" ] ; then
__curl --output "$2" "$1"
else
__curl $CURLNAMEOPTIONS "$1"
__curl --output "$2" "$URL" || return
res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__curl --output "$2" "$URL"
return
fi
__curl $CURLNAMEOPTIONS "$URL" || return
res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__curl $CURLNAMEOPTIONS "$URL"
}
check_url_http()
check_url_is_accessible()
{
local URL="$1"
__curl -I "$URL" 2>&1 | grep "HTTP" | tail -n1 | grep -q -w "200\|301"
__curl -LI "$URL" 2>&1 | grep "HTTP/" | tail -n1 | grep -q -w "200"
}
fi
# update URL variable
update_url_if_need_mirrored()
{
local MIRROR="$1"
local SECONDURL
check_url_is_accessible "$URL" && return
if [ -n "$MIRROR" ] ; then
check_url_is_accessible "$MIRROR" && URL="$MIRROR"
return
fi
MIRROR="https://mirror.eterfund.ru"
SECONDURL="$(echo "$URL" | sed -e "s|^.*://|$MIRROR/|")"
check_url_is_accessible "$SECONDURL" && URL="$SECONDURL" && return
MIRROR="https://mirror.eterfund.org"
SECONDURL="$(echo "$URL" | sed -e "s|^.*://|$MIRROR/|")"
check_url_is_accessible "$SECONDURL" && URL="$SECONDURL" && return
}
LISTONLY=''
if [ "$1" = "--list" ] ; then
LISTONLY="$1"
......@@ -226,7 +292,7 @@ fi
if [ "$1" = "--check" ] ; then
set_quiet
shift
check_url_http "$1"
check_url_is_accessible "$1"
exit
fi
......@@ -243,6 +309,12 @@ if [ "$1" = "--second-latest" ] ; then
shift
fi
CHECKMIRRORS=''
if [ "$1" = "--check-mirrors" ] ; then
CHECKMIRRORS="$1"
shift
fi
fatal()
{
echo "$*" >&2
......@@ -298,6 +370,7 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo " --check - check if URL is accessible (returns HTTP 200 OK)"
echo " --latest - print only latest version of a file"
echo " --second-latest - print only second to latest version of a file"
echo " --allow-mirrors - check mirrors if url is not accessible"
echo
echo "eget supports --list and download for https://github.com/owner/project urls"
echo
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment