Created
October 11, 2021 16:16
-
-
Save hoegaarden/ed00f46bae4c61ab856b096b8de32bc2 to your computer and use it in GitHub Desktop.
curler fooo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
set -u | |
set -o pipefail | |
URL='https://github.com/laidbackware/nsx-search/releases/download/v0.0.1/nsxsearch-darwin-0.0' | |
# URL='google.com' | |
# Should behave exaclty like curl and takes all the same args, except: | |
# - it only takes one URL, as the first argument | |
# - you cannot use `-o` but need to redirect the func's stdout instead | |
# - you must not use `-w` and override the output format | |
curler() { | |
local url="$1" | |
shift | |
local sout respCode | |
exec {sout}>&1 | |
respCode="$( | |
# does this (/dev/fd/...) work on Darwin and other *nixes? | |
curl -w "%{response_code}" -o "/dev/fd/${sout}" "${@}" "$url" || true | |
)" | |
exec {sout}>&- | |
# So if we use respCode as a return code of the funtion, then 256 would | |
# actually become 0, 257 would become 1, ... ; or in other words: 404 would | |
# become 148, 500 would become 244, ... | |
# | |
# But anyway, we have the actual http response code available here and can | |
# do whatever; Stdout & Stderr have not been mangled with but just passed | |
# on to Stdout & Stderr of the caller (and can be redirected there, if need be) | |
if [[ $respCode -lt 200 ]] || [[ $respCode -gt 299 ]] ; then | |
return "$respCode" | |
fi | |
return 0 | |
# Alternative, something like mimicking `--fail`: | |
if [[ $respCode -lt 200 ]] || [[ $respCode -gt 299 ]] ; then | |
# Stdout & Stderr have already been pushed by curl itself, here we just | |
# "append" the reponse code to the Stderr stream | |
echo "# ResponseCode: $respCode" >&2 | |
return 22 # `--fail`'s default return code | |
fi | |
return 0 | |
} | |
curlOpts=( | |
-s -L | |
# --fail | |
) | |
rc=0 | |
curler "$URL" "${curlOpts[@]}" || rc=$? | |
echo "** curler returned: $rc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment