Commit f98869a1 authored by Vitaly Lipatov's avatar Vitaly Lipatov

eget: replace grep with case in URL type checking functions

parent e2732ae8
...@@ -212,18 +212,24 @@ filter_order() ...@@ -212,18 +212,24 @@ filter_order()
have_end_slash_or_php_parametr() have_end_slash_or_php_parametr()
{ {
echo "$1" | grep -qE '(/$|\.php($|\?))' case "$1" in
*/) return 0 ;;
*.php|*.php\?*) return 0 ;;
esac
return 1
} }
is_abs_path() is_abs_path()
{ {
echo "$1" | grep -q '^/' case "$1" in /*) return 0 ;; esac
return 1
} }
is_fileurl() is_fileurl()
{ {
is_abs_path "$1" && return is_abs_path "$1" && return
echo "$1" | grep -q "^file:/" case "$1" in file:/*) return 0 ;; esac
return 1
} }
path_from_url() path_from_url()
...@@ -233,53 +239,59 @@ path_from_url() ...@@ -233,53 +239,59 @@ path_from_url()
is_url() is_url()
{ {
echo "$1" | grep -q "^[filehtps]*:/" case "$1" in file:/*|http:/*|https:/*|ftp:/*|ftps:/*|ipfs:/*) return 0 ;; esac
return 1
} }
is_strange_url() is_strange_url()
{ {
local URL="$1" local URL="$1"
is_url "$URL" || return is_url "$URL" || return
#echo "$URL" | grep -q -E "\.(deb|rpm|zip)\?" && return 1 #case "$URL" in *.deb\?*|*.rpm\?*|*.zip\?*) return 1 ;; esac
echo "$URL" | grep -q "[?&]" case "$URL" in *"?"*|*"&"*) return 0 ;; esac
return 1
} }
is_ipfs_hash() is_ipfs_hash()
{ {
# If a CID is 46 characters starting with "Qm", it's a CIDv0 # CIDv0: starts with Qm, 46 chars, alphanumeric only
echo "$1" | grep -q -E "^Qm[[:alnum:]]{44}$" && return case "$1" in Qm*) ;; *) return 1 ;; esac
case "$1" in *[!A-Za-z0-9]*) return 1 ;; esac
# TODO: CIDv1 support, see https://github.com/multiformats/cid # TODO: CIDv1 support, see https://github.com/multiformats/cid
return 1 [ "${#1}" = 46 ]
} }
is_ipfsurl() is_ipfsurl()
{ {
is_ipfs_hash "$1" && return is_ipfs_hash "$1" && return
echo "$1" | grep -q "^ipfs://" case "$1" in ipfs://*) return 0 ;; esac
return 1
} }
is_httpurl() is_httpurl()
{ {
# TODO: improve case "$1" in http://*|https://*) return 0 ;; esac
echo "$1" | grep -q "^https://" && return return 1
echo "$1" | grep -q "^http://" && return
} }
is_ftpurl() is_ftpurl()
{ {
echo "$1" | grep -q "^ftp://" case "$1" in ftp://*) return 0 ;; esac
return 1
} }
is_rsyncurl() is_rsyncurl()
{ {
echo "$1" | grep -q "^rsync://" case "$1" in rsync://*) return 0 ;; esac
return 1
} }
# SSH/rsync URL: user@host:/path or host:/path (but not scheme://) # SSH/rsync URL: user@host:/path or host:/path (but not scheme://)
is_sshurl() is_sshurl()
{ {
# Match host:path or host:/path, but not scheme:// # Exclude scheme:// first, then match host:path
echo "$1" | grep -qE '^[^/:]+:' && ! echo "$1" | grep -q "://" case "$1" in *://*) return 1 ;; *:*) return 0 ;; esac
return 1
} }
cid_from_url() cid_from_url()
...@@ -289,7 +301,8 @@ cid_from_url() ...@@ -289,7 +301,8 @@ cid_from_url()
is_numeric() is_numeric()
{ {
echo "$1" | grep -qE '^[0-9]+$' case "$1" in *[!0-9]*|'') return 1 ;; esac
return 0
} }
# Calculate average with outlier filtering (2σ rule) # Calculate average with outlier filtering (2σ rule)
......
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