Commit 25e445f9 authored by Vitaly Lipatov's avatar Vitaly Lipatov

eget: reorder the code

parent e7b3ba6d
......@@ -104,43 +104,35 @@ docmd()
"$@"
}
check_tty
WGETNOSSLCHECK=''
CURLNOSSLCHECK=''
WGETUSERAGENT=''
CURLUSERAGENT=''
WGETQ='' #-q
CURLQ='' #-s
WGETNAMEOPTIONS='--content-disposition'
CURLNAMEOPTIONS='--remote-name --remote-header-name'
set_quiet()
# copied from epm
# print a path to the command if exists in $PATH
if which which 2>/dev/null >/dev/null ; then
# the best case if we have which command (other ways needs checking)
# TODO: don't use which at all, it is binary, not builtin shell command
print_command_path()
{
WGETQ='-q'
CURLQ='-s'
which -- "$1" 2>/dev/null
}
elif type -a type 2>/dev/null >/dev/null ; then
print_command_path()
{
type -fpP -- "$1" 2>/dev/null
}
else
print_command_path()
{
type "$1" 2>/dev/null | sed -e 's|.* /|/|'
}
# TODO: parse options in a good way
# TODO: passthrou all wget options
if [ "$1" = "-q" ] ; then
set_quiet
shift
fi
if [ "$1" = "-k" ] || [ "$1" = "--no-check-certificate" ] ; then
WGETNOSSLCHECK='--no-check-certificate'
CURLNOSSLCHECK='-k'
shift
fi
# check if <arg> is a real command
is_command()
{
print_command_path "$1" >/dev/null
}
if [ "$1" = "-U" ] || [ "$1" = "-A" ] || [ "$1" = "--user-agent" ] ; then
user_agent="Mozilla/5.0 (X11; Linux $arch) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
WGETUSERAGENT="-U '$user_agent'"
CURLUSERAGENT="-A '$user_agent'"
shift
fi
# check man glob
filter_glob()
......@@ -203,6 +195,82 @@ download_with_mirroring()
}
check_tty
WGETNOSSLCHECK=''
CURLNOSSLCHECK=''
WGETUSERAGENT=''
CURLUSERAGENT=''
WGETQ='' #-q
CURLQ='' #-s
WGETNAMEOPTIONS='--content-disposition'
CURLNAMEOPTIONS='--remote-name --remote-header-name'
set_quiet()
{
WGETQ='-q'
CURLQ='-s'
}
# TODO: parse options in a good way
# TODO:
# -P support
if [ -z "$1" ] ; then
echo "eget - wget like downloader wrapper with wildcard support, uses wget or curl as backend" >&2
fatal "Run $0 --help to get help"
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "eget - wget like downloader wrapper with wildcard support in filename part of URL"
echo "Usage: eget [-q] [-k] [-U] [-O target file] [--list] http://somesite.ru/dir/na*.log"
echo
echo "Options:"
echo " -q - quiet mode"
echo " -k|--no-check-certificate - skip SSL certificate chain support"
echo " -U|-A|--user-agent - send browser like UserAgent"
echo " -O file - download to this file (use filename from server if missed)"
echo " --list - print files from url with mask"
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
echo "Examples:"
echo " $ eget --list http://ftp.somesite.ru/package-*.tar"
echo " $ eget http://ftp.somesite.ru/package-*.x64.tar"
echo " $ eget --check http://ftp.somesite.ru/test"
echo " $ eget --list http://download.somesite.ru 'package-*.tar.xz'"
echo " $ eget --list --latest https://github.com/telegramdesktop/tdesktop/releases 'tsetup.*.tar.xz'"
# echo "See $ wget --help for wget options you can use here"
exit
fi
# TODO: passthrou all wget options
if [ "$1" = "-q" ] ; then
set_quiet
shift
fi
if [ "$1" = "-k" ] || [ "$1" = "--no-check-certificate" ] ; then
WGETNOSSLCHECK='--no-check-certificate'
CURLNOSSLCHECK='-k'
shift
fi
if [ "$1" = "-U" ] || [ "$1" = "-A" ] || [ "$1" = "--user-agent" ] ; then
user_agent="Mozilla/5.0 (X11; Linux $arch) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
WGETUSERAGENT="-U '$user_agent'"
CURLUSERAGENT="-A '$user_agent'"
shift
fi
LISTONLY=''
if [ "$1" = "--list" ] ; then
LISTONLY="$1"
......@@ -246,7 +314,7 @@ fi
WGET="$(which wget 2>/dev/null)"
WGET="$(print_command_path wget)"
if is_fileurl "$1" ; then
......@@ -317,7 +385,7 @@ check_url_is_accessible()
}
else
CURL="$(which curl 2>/dev/null)"
CURL="$(print_command_path curl)"
[ -n "$CURL" ] || fatal "There are no wget nor curl in the system. Install it with $ epm install curl"
__curl()
{
......@@ -358,42 +426,6 @@ check_url_is_accessible()
fi
# TODO:
# -P support
if [ -z "$1" ] ; then
echo "eget - wget like downloader wrapper with wildcard support, uses wget or curl as backend" >&2
fatal "Run $0 --help to get help"
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "eget - wget like downloader wrapper with wildcard support in filename part of URL"
echo "Usage: eget [-q] [-k] [-U] [-O target file] [--list] http://somesite.ru/dir/na*.log"
echo
echo "Options:"
echo " -q - quiet mode"
echo " -k|--no-check-certificate - skip SSL certificate chain support"
echo " -U|-A|--user-agent - send browser like UserAgent"
echo " -O file - download to this file (use filename from server if missed)"
echo " --list - print files from url with mask"
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
echo "Examples:"
echo " $ eget --list http://ftp.somesite.ru/package-*.tar"
echo " $ eget http://ftp.somesite.ru/package-*.x64.tar"
echo " $ eget --check http://ftp.somesite.ru/test"
echo " $ eget --list http://download.somesite.ru 'package-*.tar.xz'"
echo " $ eget --list --latest https://github.com/telegramdesktop/tdesktop/releases 'tsetup.*.tar.xz'"
# echo "See $ wget --help for wget options you can use here"
exit
fi
get_github_urls()
{
# https://github.com/OWNER/PROJECT
......@@ -406,69 +438,6 @@ get_github_urls()
grep -i -o -E '"browser_download_url": "https://.*"' | cut -d'"' -f4
}
if [ -n "$CHECKURL" ] ; then
set_quiet
check_url_is_accessible "$1"
exit
fi
if echo "$1" | grep -q "^https://github.com/" && \
echo "$1" | grep -q -v "/download/" && [ -n "$2" ] ; then
MASK="$2"
if [ -n "$LISTONLY" ] ; then
get_github_urls "$1" | filter_glob "$MASK" | filter_order
exit
fi
ERROR=0
for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
sget "$fn" "$TARGETFILE" || ERROR=1
[ -n "$TARGETFILE" ] && [ "$ERROR" = "0" ] && break
done
exit
fi
# do not support /
if echo "$1" | grep -q "/$" && [ -z "$2" ] ; then
fatal "Use http://example.com/e/* to download all files in dir"
fi
# TODO: curl?
# If ftp protocol, just download
if echo "$1" | grep -q "^ftp://" ; then
[ -n "$LISTONLY" ] && fatal "TODO: list files for ftp:// do not supported yet"
sget "$1" "$TARGETFILE"
exit
fi
# mask allowed only in the last part of path
MASK=$(basename "$1")
# if mask are second arg
if [ -n "$2" ] ; then
URL="$1"
MASK="$2"
else
# drop mask part
URL="$(dirname "$1")/"
fi
if [ -z "$MASK" ] && echo "$URL" | grep -q "[*?]" ; then
fatal "Error: there are globbing symbols (*?) in $URL"
fi
# If have no wildcard symbol like asterisk, just download
if echo "$MASK" | grep -qv "[*?]" || echo "$MASK" | grep -q "[?].*="; then
if is_fileurl "$1" ; then
cp -v "$(dir_from_url)" "$TARGEFILE"
exit
fi
sget "$1" "$TARGETFILE"
exit
fi
# drop file path from URL
get_host_only()
{
......@@ -516,6 +485,74 @@ get_urls()
grep -i -o -E 'href="(.+)"' | cut -d'"' -f2
}
if [ -n "$CHECKURL" ] ; then
set_quiet
check_url_is_accessible "$1"
exit
fi
if echo "$1" | grep -q "^https://github.com/" && \
echo "$1" | grep -q -v "/download/" && [ -n "$2" ] ; then
MASK="$2"
if [ -n "$LISTONLY" ] ; then
get_github_urls "$1" | filter_glob "$MASK" | filter_order
exit
fi
ERROR=0
for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
sget "$fn" "$TARGETFILE" || ERROR=1
[ -n "$TARGETFILE" ] && [ "$ERROR" = "0" ] && break
done
exit
fi
# mask allowed only in the last part of path
MASK=$(basename "$1")
# if mask is the second arg
if [ -n "$2" ] ; then
URL="$1"
MASK="$2"
else
# do not support / at the end without separately specified mask
if echo "$1" | grep -q "/$" ; then
fatal "Use http://example.com/e/* to download all files in dir"
fi
# drop mask part
URL="$(dirname "$1")/"
fi
if echo "$URL" | grep -q "[*?]" ; then
fatal "Error: there are globbing symbols (*?) in $URL. It is allowed only for mask part"
fi
# TODO: curl?
# If ftp protocol, just download
if echo "$URL" | grep -q "^ftp://" ; then
[ -n "$LISTONLY" ] && fatal "TODO: list files for ftp:// is not supported yet"
sget "$1" "$TARGETFILE"
exit
fi
# If there is no wildcard symbol like asterisk, just download
if echo "$MASK" | grep -qv "[*?]" || echo "$MASK" | grep -q "[?].*="; then
if is_fileurl "$1" ; then
cp -v "$(dir_from_url)" "$TARGEFILE"
exit
fi
sget "$1" "$TARGETFILE"
exit
fi
if [ -n "$LISTONLY" ] ; then
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
is_url "$fn" && echo "$fn" && continue
......
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