Skip to content

Commit 1ce243a

Browse files
committed
Capture response bodies in $REPLY
1 parent f55d51f commit 1ce243a

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

bin/gitea

+5-5
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ api() {
258258
# extglob is needed for pattern matching
259259
local r=0; shopt -s extglob; api "$@" || r=$?; shopt -u extglob; return $r
260260
fi
261-
read-curl --silent --write-out '%{http_code}' --output /dev/null "${@:4}" "${GITEA_URL%/}/api/v1/${3#/}"
262-
local true="@($1)" false="@($2)"
261+
read-curl --silent --write-out '\n%{http_code}' "${@:4}" "${GITEA_URL%/}/api/v1/${3#/}"
262+
local true="@($1)" false="@($2)" code=${REPLY##*$'\n'}; REPLY=${REPLY%$code}
263263
# shellcheck disable=2053 # glob matching is what we want!
264-
if [[ $REPLY == $true ]]; then return 0; elif [[ $2 && $REPLY == $false ]]; then return 1
265-
else case $REPLY in
264+
if [[ $code == $true ]]; then return 0; elif [[ $2 && $code == $false ]]; then return 1
265+
else case $code in
266266
000) fail "Invalid server response: check GITEA_URL" 78 ;; # EX_PROTOCOL
267267
401) fail "Unauthorized: check GITEA_USER and GITEA_API_TOKEN" 77 ;; # EX_NOPERM
268268
404) fail "Server returned 404 Not Found" 69 ;; # EX_UNAVAILABLE
269-
*) fail "Failure: HTTP code $REPLY (expected $1${2:+ or $2})" 70 ;; # EX_SOFTWARE
269+
*) fail "Failure: HTTP code $code (expected $1${2:+ or $2})" 70 ;; # EX_SOFTWARE
270270
esac
271271
fi
272272
}

gitea.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ gitea.exists() { split_repo "$1" && auth api 200 404 "repos/$REPLY" ; }
197197
198198
~~~shell
199199
$ gitea exists foo/bar </dev/null; echo [$?]
200-
curl --silent --write-out %\{http_code\} --output /dev/null -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
200+
curl --silent --write-out \\n%\{http_code\} -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
201201
[0]
202202
$ curl_status=404 gitea exists foo/bar </dev/null; echo [$?]
203-
curl --silent --write-out %\{http_code\} --output /dev/null -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
203+
curl --silent --write-out \\n%\{http_code\} -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
204204
[1]
205205
~~~
206206
@@ -214,11 +214,11 @@ gitea.delete() { split_repo "$1" && auth api 204 "" "/repos/$REPLY" -X DELETE; }
214214
215215
~~~shell
216216
$ gitea delete foo/bar </dev/null
217-
curl --silent --write-out %\{http_code\} --output /dev/null -X DELETE -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
217+
curl --silent --write-out \\n%\{http_code\} -X DELETE -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
218218
Failure: HTTP code 200 (expected 204)
219219
[70]
220220
$ curl_status=204 gitea delete foo/bar </dev/null; echo [$?]
221-
curl --silent --write-out %\{http_code\} --output /dev/null -X DELETE -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
221+
curl --silent --write-out \\n%\{http_code\} -X DELETE -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar
222222
[0]
223223
~~~
224224
@@ -235,7 +235,7 @@ gitea.deploy-key() {
235235
236236
~~~shell
237237
$ curl_status=201 gitea deploy-key foo/bar baz spam false
238-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
238+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
239239
{
240240
"title": "baz",
241241
"key": "spam",
@@ -263,7 +263,7 @@ gitea.new() {
263263
# Defaults apply before command line; default API url is /org/ORGNAME/repos
264264
$ export GIT_AUTHOR_NAME="PJ Eby" EMAIL="[email protected]"
265265
$ gitea --private new biz/baz description whatever
266-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/biz/repos\?token=EXAMPLE_TOKEN
266+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/biz/repos\?token=EXAMPLE_TOKEN
267267
{
268268
"name": "baz",
269269
"private": true,
@@ -273,7 +273,7 @@ gitea.new() {
273273
# When the repo is the current user, the API url is /user/repos
274274
$ GITEA_CREATE=(private= true)
275275
$ gitea --public -d something new some_user/spam
276-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/user/repos\?token=EXAMPLE_TOKEN
276+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/user/repos\?token=EXAMPLE_TOKEN
277277
{
278278
"name": "spam",
279279
"private": false,
@@ -282,12 +282,12 @@ gitea.new() {
282282
283283
# Deployment happens if you provide a GITEA_DEPLOY_KEY
284284
$ GITEA_DEPLOY_KEY=example-key curl_status=201 gitea new foo/bar
285-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/foo/repos\?token=EXAMPLE_TOKEN
285+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/foo/repos\?token=EXAMPLE_TOKEN
286286
{
287287
"name": "bar",
288288
"private": true
289289
}
290-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
290+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
291291
{
292292
"title": "default",
293293
"key": "example-key",
@@ -300,12 +300,12 @@ gitea.new() {
300300
> GITEA_DEPLOY_READONLY=false \
301301
> curl_status=201 \
302302
> gitea new foo/bar
303-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/foo/repos\?token=EXAMPLE_TOKEN
303+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/foo/repos\?token=EXAMPLE_TOKEN
304304
{
305305
"name": "bar",
306306
"private": true
307307
}
308-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
308+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
309309
{
310310
"title": "sample-title",
311311
"key": "example-key",
@@ -365,8 +365,8 @@ gitea.vendor() {
365365
# New Repository
366366
$ mkdir foo; cd foo; echo "v1" >f
367367
$ curl_status=404,200 gitea -p -r foo -t 1.1 vendor </dev/null 2>&1|grep -v '^ Author:'
368-
curl --silent --write-out %\{http_code\} --output /dev/null -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/some_user/foo
369-
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/user/repos\?token=EXAMPLE_TOKEN
368+
curl --silent --write-out \\n%\{http_code\} -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/some_user/foo
369+
curl --silent --write-out \\n%\{http_code\} -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/user/repos\?token=EXAMPLE_TOKEN
370370
{
371371
"name": "foo",
372372
"private": false
@@ -389,7 +389,7 @@ gitea.vendor() {
389389
$ rm -rf .git
390390
$ echo "v2" >>f
391391
$ gitea -r foo -t 1.2 vendor </dev/null 2>&1|grep -v '^ Author:'
392-
curl --silent --write-out %\{http_code\} --output /dev/null -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/some_user/foo
392+
curl --silent --write-out \\n%\{http_code\} -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/some_user/foo
393393
Cloning into bare repository '.git'...
394394
done.
395395
[vendor *] Vendor update to 1.2 (glob)
@@ -493,15 +493,15 @@ api() {
493493
# extglob is needed for pattern matching
494494
local r=0; shopt -s extglob; api "$@" || r=$?; shopt -u extglob; return $r
495495
fi
496-
read-curl --silent --write-out '%{http_code}' --output /dev/null "${@:4}" "${GITEA_URL%/}/api/v1/${3#/}"
497-
local true="@($1)" false="@($2)"
496+
read-curl --silent --write-out '\n%{http_code}' "${@:4}" "${GITEA_URL%/}/api/v1/${3#/}"
497+
local true="@($1)" false="@($2)" code=${REPLY##*$'\n'}; REPLY=${REPLY%$code}
498498
# shellcheck disable=2053 # glob matching is what we want!
499-
if [[ $REPLY == $true ]]; then return 0; elif [[ $2 && $REPLY == $false ]]; then return 1
500-
else case $REPLY in
499+
if [[ $code == $true ]]; then return 0; elif [[ $2 && $code == $false ]]; then return 1
500+
else case $code in
501501
000) fail "Invalid server response: check GITEA_URL" 78 ;; # EX_PROTOCOL
502502
401) fail "Unauthorized: check GITEA_USER and GITEA_API_TOKEN" 77 ;; # EX_NOPERM
503503
404) fail "Server returned 404 Not Found" 69 ;; # EX_UNAVAILABLE
504-
*) fail "Failure: HTTP code $REPLY (expected $1${2:+ or $2})" 70 ;; # EX_SOFTWARE
504+
*) fail "Failure: HTTP code $code (expected $1${2:+ or $2})" 70 ;; # EX_SOFTWARE
505505
esac
506506
fi
507507
}
@@ -511,34 +511,34 @@ fail() { echo "$1" >&2; return "${2-64}"; }
511511
512512
~~~shell
513513
$ api 200 "" x extra args </dev/null; echo [$?]
514-
curl --silent --write-out %\{http_code\} --output /dev/null extra args https://example.com/gitea/api/v1/x
514+
curl --silent --write-out \\n%\{http_code\} extra args https://example.com/gitea/api/v1/x
515515
[0]
516516
517517
$ api "" "400|200" x extra args </dev/null; echo [$?]
518-
curl --silent --write-out %\{http_code\} --output /dev/null extra args https://example.com/gitea/api/v1/x
518+
curl --silent --write-out \\n%\{http_code\} extra args https://example.com/gitea/api/v1/x
519519
[1]
520520
521521
$ curl_status=000 api 200 "" x extra args </dev/null; echo [$?]
522-
curl --silent --write-out %\{http_code\} --output /dev/null extra args https://example.com/gitea/api/v1/x
522+
curl --silent --write-out \\n%\{http_code\} extra args https://example.com/gitea/api/v1/x
523523
Invalid server response: check GITEA_URL
524524
[78]
525525
526526
$ curl_status=401 api 200 "" /y </dev/null
527-
curl --silent --write-out %\{http_code\} --output /dev/null https://example.com/gitea/api/v1/y
527+
curl --silent --write-out \\n%\{http_code\} https://example.com/gitea/api/v1/y
528528
Unauthorized: check GITEA_USER and GITEA_API_TOKEN
529529
[77]
530530
531531
$ curl_status=404 api 200 401 /z </dev/null; echo [$?]
532-
curl --silent --write-out %\{http_code\} --output /dev/null https://example.com/gitea/api/v1/z
532+
curl --silent --write-out \\n%\{http_code\} https://example.com/gitea/api/v1/z
533533
Server returned 404 Not Found
534534
[69]
535535
536536
$ curl_status=404 api 200 404 /z </dev/null; echo [$?]
537-
curl --silent --write-out %\{http_code\} --output /dev/null https://example.com/gitea/api/v1/z
537+
curl --silent --write-out \\n%\{http_code\} https://example.com/gitea/api/v1/z
538538
[1]
539539
540540
$ curl_status=501 api 200 401 /z </dev/null; echo [$?]
541-
curl --silent --write-out %\{http_code\} --output /dev/null https://example.com/gitea/api/v1/z
541+
curl --silent --write-out \\n%\{http_code\} https://example.com/gitea/api/v1/z
542542
Failure: HTTP code 501 (expected 200 or 401)
543543
[70]
544544
~~~

0 commit comments

Comments
 (0)