Commit a855c33d authored by Vitaly Lipatov's avatar Vitaly Lipatov

tools_eget: update to eget 4.0 (wget/curl support)

parent f6a96334
...@@ -21,17 +21,69 @@ ...@@ -21,17 +21,69 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
WGET="wget" fatal()
{
echo "FATAL: $*" >&2
exit 1
}
WGETQ='' #-q
CURLQ='' #-s
set_quiet()
{
WGETQ='-q'
CURLQ='-s'
}
# TODO: passthrou all wget options # TODO: passthrou all wget options
if [ "$1" = "-q" ] ; then if [ "$1" = "-q" ] ; then
WGET="wget -q" set_quiet
shift shift
fi fi
WGET="$(which wget 2>/dev/null)"
if [ -n "$WGET" ] ; then
# put remote content to stdout
scat()
{
$WGET $WGETQ -O- "$1"
}
# download to default name of to $2
sget()
{
if [ -n "$2" ] ; then
$WGET $WGETQ -O "$2" "$1"
else
$WGET $WGETQ "$1"
fi
}
else
CURL="$(which curl 2>/dev/null)"
[ -n "$CURL" ] || fatal "There are no wget nor curl in the system. Install it with $ epm install curl"
# put remote content to stdout
scat()
{
$CURL -L $CURLQ "$1"
}
# download to default name of to $2
sget()
{
if [ -n "$2" ] ; then
$CURL -L $CURLQ --output "$2" "$1"
else
$CURL -L $CURLQ -O "$1"
fi
}
fi
LISTONLY='' LISTONLY=''
if [ "$1" = "--list" ] ; then if [ "$1" = "--list" ] ; then
LISTONLY="$1" LISTONLY="$1"
set_quiet
shift shift
fi fi
...@@ -62,10 +114,9 @@ filter_order() ...@@ -62,10 +114,9 @@ filter_order()
} }
# download to this file # download to this file
WGET_OPTION_TARGET= TARGETFILE=''
if [ "$1" = "-O" ] ; then if [ "$1" = "-O" ] ; then
TARGETFILE="$2" TARGETFILE="$2"
WGET_OPTION_TARGET="-O $2"
shift 2 shift 2
fi fi
...@@ -73,21 +124,23 @@ fi ...@@ -73,21 +124,23 @@ fi
# -P support # -P support
if [ -z "$1" ] ; then if [ -z "$1" ] ; then
echo "eget - wget wrapper" >&2 echo "eget - wget like downloader" >&2
fatal "Run with URL, like http://somesite.ru/dir/*.log" fatal "Run $0 --help to get help"
fi fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "eget - wget wrapper, with support" echo "eget - wget like downloader with wildcard support in filename part of URL"
echo "Usage: eget [-O target file] [--list] http://somesite.ru/dir/na*.log" echo "Usage: eget [-q] [-O target file] [--list] http://somesite.ru/dir/na*.log"
echo echo
echo "Options:" echo "Options:"
echo " --list - print files frm url with mask" echo " -q - quiet mode"
echo " -O file - download to this file (use filename from server if missed)"
echo " --list - print files from url with mask"
echo " --latest - print only latest version of file" echo " --latest - print only latest version of file"
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
echo "See $ wget --help for wget options you can use here" # echo "See $ wget --help for wget options you can use here"
exit exit
fi fi
...@@ -99,9 +152,7 @@ get_github_urls() ...@@ -99,9 +152,7 @@ get_github_urls()
[ -n "$owner" ] || fatal "Can't get owner from $1" [ -n "$owner" ] || fatal "Can't get owner from $1"
[ -n "$project" ] || fatal "Can't get project from $1" [ -n "$project" ] || fatal "Can't get project from $1"
local URL="https://api.github.com/repos/$owner/$project/releases/latest" local URL="https://api.github.com/repos/$owner/$project/releases/latest"
local q='' scat $URL | \
[ -n "$LISTONLY" ] && q="-q"
$WGET $q -O- $URL | \
grep -i -o -E '"browser_download_url": "https://.*"' | cut -d'"' -f4 grep -i -o -E '"browser_download_url": "https://.*"' | cut -d'"' -f4
} }
...@@ -114,7 +165,7 @@ if echo "$1" | grep -q "^https://github.com/" ; then ...@@ -114,7 +165,7 @@ if echo "$1" | grep -q "^https://github.com/" ; then
fi fi
for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do for fn in $(get_github_urls "$1" | filter_glob "$MASK" | filter_order) ; do
$WGET "$fn" || ERROR=1 sget "$fn" || ERROR=1
done done
exit exit
fi fi
...@@ -127,8 +178,8 @@ fi ...@@ -127,8 +178,8 @@ fi
# If ftp protocol, just download # If ftp protocol, just download
if echo "$1" | grep -q "^ftp://" ; then if echo "$1" | grep -q "^ftp://" ; then
[ -n "$LISTONLY" ] && fatal "Error: list files for ftp:// do not supported yet" [ -n "$LISTONLY" ] && fatal "TODO: list files for ftp:// do not supported yet"
$WGET $WGET_OPTION_TARGET "$1" sget "$1" "$TARGETFILE"
exit exit
fi fi
...@@ -139,24 +190,24 @@ if echo "$URL" | grep -q "[*?]" ; then ...@@ -139,24 +190,24 @@ if echo "$URL" | grep -q "[*?]" ; then
fatal "Error: there are globbing symbols (*?) in $URL" fatal "Error: there are globbing symbols (*?) in $URL"
fi fi
# mask allowed only in last part of path # mask allowed only in the last part of path
MASK=$(basename "$1") MASK=$(basename "$1")
# If have no wildcard symbol like asterisk, just download # If have no wildcard symbol like asterisk, just download
if echo "$MASK" | grep -qv "[*?]" ; then if echo "$MASK" | grep -qv "[*?]" ; then
$WGET $WGET_OPTION_TARGET "$1" sget "$1" "$TARGETFILE"
exit exit
fi fi
get_urls() get_urls()
{ {
$WGET -O- $URL | \ scat $URL | \
grep -i -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2 grep -i -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2
} }
if [ -n "$LISTONLY" ] ; then if [ -n "$LISTONLY" ] ; then
WGET="$WGET -q"
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
# TODO: return full url? someone use old behaviour?
echo "$(basename "$fn")" echo "$(basename "$fn")"
done done
exit exit
...@@ -164,7 +215,7 @@ fi ...@@ -164,7 +215,7 @@ fi
ERROR=0 ERROR=0
for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do for fn in $(get_urls | filter_glob "$MASK" | filter_order) ; do
$WGET "$URL/$(basename "$fn")" || ERROR=1 sget "$URL/$(basename "$fn")" || ERROR=1
done done
exit $ERROR exit $ERROR
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