-
Notifications
You must be signed in to change notification settings - Fork 378
/
common.shrc
48 lines (43 loc) · 1.24 KB
/
common.shrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Simple cURL wrapper to manage nicely error handling:
#
# * In case of success, just read body from stdout
# * In case of HTTP error (status >= 400), first stderr contains "HTTP status: XXX", then body
# * In case of other error, just print cURL error on stderr
#
# This function requires a temporary file. It's created under ${TEMP_DIR} if defined and not empty.
# Otherwise, it relies on `mktemp` defaults.
#
curl.do() {
local rc=0
local mktemp_opts=( '--suffix=.curl' )
[[ -z "${TEMP_DIR}" ]] || mktemp_opts+=( "--tempdir=${TEMP_DIR}" )
local curl_body_file=''
curl_body_file="$(mktemp "${mktemp_opts[@]}")" || {
rc=$?
echo "Unable to create temporary file for cURL output"
return $rc
} >&2
local curl_opts=(
--output "${curl_body_file}"
--write-out '%{http_code}'
--silent
--show-error
"$@"
)
local http_code=''
http_code="$(curl "${curl_opts[@]}")" || rc=$?
(( http_code < 400 )) || {
(( rc == 0 )) || rc=1
echo "HTTP status: ${http_code}"
} >&2
if [[ $rc == 0 ]]; then
cat "${curl_body_file}" || rc=$?
else
cat "${curl_body_file}" >&2
fi
rm -rf "${curl_body_file}" || {
(( rc == 0 )) || rc=1
echo "Unable to clear temporary file '${curl_body_file}'"
} >&2
return $rc
}