From 31ecd7b7123e83f24a73fd0908b47836c5bb26f7 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Wed, 24 Apr 2013 08:57:09 +0300 Subject: dev-shell: improved error handling --- dev-shell | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'dev-shell') diff --git a/dev-shell b/dev-shell index 9e64500..eaacde9 100755 --- a/dev-shell +++ b/dev-shell @@ -3,16 +3,33 @@ # Copyright (c) 2012-2013 Kaarle Ritvanen # See LICENSE file for license details +function parse_var { + echo "$2" | sed "s/^$1: \\(.\\+\\)"$'\r'"\$/\\1/;ta;d;:a;q" +} + function _acf_req { local url=$ACF_URL$1 shift - curl -s \ + + local resp + resp=$(curl -s -w $'\n\nStatus: %{response_code}\r\n' \ ${ACF_AUTH_TOKEN:+-H "X-ACF-Auth-Token: $ACF_AUTH_TOKEN"} \ ${ACF_TXN_ID:+-H "X-ACF-Transaction-ID: $ACF_TXN_ID"} \ - "$@" $url + "$@" $url) local code=$? - echo - return $code + + if [ $code -gt 0 ]; then + echo "Request failed, code $code" >&2 + return 1 + fi + + echo "$resp" + + local status=$(parse_var Status "$resp") + [ "${status:0:1}" = 2 ] && return + + echo "Request failed" >&2 + return 1 } function _acf_start_req { @@ -22,10 +39,15 @@ function _acf_start_req { shift 3 local current=${!var} - eval "export $var=\$(_acf_req \"\$url\" \"\$@\" \ - -D /proc/self/fd/1 -o /proc/self/fd/3 | \ - sed 's/^$hdr: \\(.\\+\\)'\$'\\r''\$/\\1/;ta;d;:a;q')" - [ "${!var}" ] || exit + eval $var= + local status= + + local resp + resp=$(_acf_req "$url" "$@" -D /proc/self/fd/1 -o /proc/self/fd/3) + [ $? -eq 0 ] || return + + local id=$(parse_var $hdr "$resp") + eval export $var=$id bash --rcfile "$ACF_QD_CLI" && _acf_req $url -X DELETE -- cgit v1.2.3