From 23bf3723bd2c28dc12400afcd05e28eacdb380cb Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 22:41:48 -0500 Subject: [PATCH 1/3] Identity function from jq should not change the output --- bin/common.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/common.sh b/bin/common.sh index bd5e9b5..c65ea17 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -60,7 +60,7 @@ curlgh_all_pages_status () { page=1 present='true' while [ "$present" = "true" ]; do - current_results=$(curlgh "$@?page=$page" | jq .) + current_results=$(curlgh "$@?page=$page") if [ "$(echo $current_results | jq .statuses)" != "[]" ]; then results=$(jq -s '.[0] as $o1 | .[1] as $o2 | ($o1 + $o2) | .statuses = ($o1.statuses + $o2.statuses)' <(echo $results) <(echo $current_results)) page=$(($page+1)) From 320683ef5688025eaa42797329cc85d1eefb1b2c Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 23:05:04 -0500 Subject: [PATCH 2/3] Alternative method for combining statuses array across multiple queries --- bin/common.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/common.sh b/bin/common.sh index c65ea17..c084b4c 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -61,8 +61,12 @@ curlgh_all_pages_status () { present='true' while [ "$present" = "true" ]; do current_results=$(curlgh "$@?page=$page") - if [ "$(echo $current_results | jq .statuses)" != "[]" ]; then - results=$(jq -s '.[0] as $o1 | .[1] as $o2 | ($o1 + $o2) | .statuses = ($o1.statuses + $o2.statuses)' <(echo $results) <(echo $current_results)) + + # If key "statuses" is not present, stop iterating loop + statuses=$(echo $current_results | jq -c '.statuses // []') + if [ "$statuses" != "[]" ]; then + # Identify "statuses" array in `current_results` and append it to "statuses" array in `results` + results=$(echo "$results" | jq --argjson s "$statuses" '.statuses += $s') page=$(($page+1)) else present='false' From 507e2b1e73cd2414d48f5db3cdff1c83f318af67 Mon Sep 17 00:00:00 2001 From: Dennis Wong Date: Thu, 27 Feb 2020 23:08:34 -0500 Subject: [PATCH 3/3] Save inital response so there is an array to append to in future iterations of loop --- bin/common.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/common.sh b/bin/common.sh index c084b4c..fb98a35 100644 --- a/bin/common.sh +++ b/bin/common.sh @@ -57,17 +57,25 @@ curlgh () { curlgh_all_pages_status () { results='' - page=1 + page=0 present='true' while [ "$present" = "true" ]; do + page=$(($page+1)) current_results=$(curlgh "$@?page=$page") + # Save the first query as the return value, in case the response + # body is not expected, the response body can still be returned + # and behave like curlgh would in the non-happy path. + if [ -z "$results" ]; then + results="$current_results" + continue + fi + # If key "statuses" is not present, stop iterating loop statuses=$(echo $current_results | jq -c '.statuses // []') if [ "$statuses" != "[]" ]; then # Identify "statuses" array in `current_results` and append it to "statuses" array in `results` results=$(echo "$results" | jq --argjson s "$statuses" '.statuses += $s') - page=$(($page+1)) else present='false' fi