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

eget: implement --check-mirrors

parent 623ad293
...@@ -153,31 +153,53 @@ __wget() ...@@ -153,31 +153,53 @@ __wget()
docmd $WGET $WGETQ $WGETNOSSLCHECK "$@" docmd $WGET $WGETQ $WGETNOSSLCHECK "$@"
fi fi
} }
# put remote content to stdout # put remote content to stdout
scat() 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 # download to default name of to $2
sget() sget()
{ {
local URL="$1"
local res
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
scat "$1" scat "$URL"
return
elif [ -n "$2" ] ; then elif [ -n "$2" ] ; then
__wget -O "$2" "$1" __wget -O "$2" "$URL" && return
else res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__wget -O "$2" "$URL"
return
fi
# TODO: поддержка rsync для известных хостов? # TODO: поддержка rsync для известных хостов?
# Не качать, если одинаковый размер и дата # Не качать, если одинаковый размер и дата
# -nc # -nc
# TODO: overwrite always # TODO: overwrite always
__wget $WGETNAMEOPTIONS "$1" __wget $WGETNAMEOPTIONS "$URL" && return
fi res=$?
[ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__wget $WGETNAMEOPTIONS "$URL"
} }
check_url_http() check_url_is_accessible()
{ {
local URL="$1" 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 else
...@@ -194,28 +216,72 @@ __curl() ...@@ -194,28 +216,72 @@ __curl()
# put remote content to stdout # put remote content to stdout
scat() 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 # download to default name of to $2
sget() sget()
{ {
local URL="$1"
local res
if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then if [ "$2" = "/dev/stdout" ] || [ "$2" = "-" ] ; then
scat "$1" scat "$1"
return
elif [ -n "$2" ] ; then elif [ -n "$2" ] ; then
__curl --output "$2" "$1" __curl --output "$2" "$URL" || return
else res=$?
__curl $CURLNAMEOPTIONS "$1" [ -n "$CHECKMIRRORS" ] || return $res
update_url_if_need_mirrored || return
__curl --output "$2" "$URL"
return
fi 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" 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 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='' LISTONLY=''
if [ "$1" = "--list" ] ; then if [ "$1" = "--list" ] ; then
LISTONLY="$1" LISTONLY="$1"
...@@ -226,7 +292,7 @@ fi ...@@ -226,7 +292,7 @@ fi
if [ "$1" = "--check" ] ; then if [ "$1" = "--check" ] ; then
set_quiet set_quiet
shift shift
check_url_http "$1" check_url_is_accessible "$1"
exit exit
fi fi
...@@ -243,6 +309,12 @@ if [ "$1" = "--second-latest" ] ; then ...@@ -243,6 +309,12 @@ if [ "$1" = "--second-latest" ] ; then
shift shift
fi fi
CHECKMIRRORS=''
if [ "$1" = "--check-mirrors" ] ; then
CHECKMIRRORS="$1"
shift
fi
fatal() fatal()
{ {
echo "$*" >&2 echo "$*" >&2
...@@ -298,6 +370,7 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then ...@@ -298,6 +370,7 @@ if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo " --check - check if URL is accessible (returns HTTP 200 OK)" echo " --check - check if URL is accessible (returns HTTP 200 OK)"
echo " --latest - print only latest version of a file" echo " --latest - print only latest version of a file"
echo " --second-latest - print only second to 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
echo "eget supports --list and download for https://github.com/owner/project urls" echo "eget supports --list and download for https://github.com/owner/project urls"
echo 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