#! /usr/bin/env bash set -o errtrace set -o nounset set -o pipefail shopt -s inherit_errexit trap 'echo $LINENO: $BASH_COMMAND; exit 1' ERR : "${DOMAIN:?specify domain name}" readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN:?not set error}" readonly QUERY='query AllServices { services { allServices { id isEnabled status } } }' : "${EXPECTED_RESPONSE_FILE:?not set error}" # shellcheck disable=SC2116 # shellcheck disable=SC2086 REQUEST=$(cat << EOF { "query": "$(echo $QUERY)", "variables": null, "operationName": "AllServices" } EOF ) readonly REQUEST curl_cmd=(curl --fail-with-body --show-error -s "https://api.$DOMAIN/graphql" --compressed -H 'Content-Type: application/json' --data-raw "$REQUEST" -k -H "$AUTH_HEADER") printf "%s\n" "${curl_cmd[*]}" received_response="$("${curl_cmd[@]}")" sorted_response="$(jq --sort-keys <<<"$received_response")" \ || { echo "error"; echo "$received_response"; echo -e "QUERY:\n$QUERY"; exit 1; } jq <<<"$sorted_response" if diff -w -u --color=always "$EXPECTED_RESPONSE_FILE" <(printf "%s\n" "$sorted_response") 2>/dev/null then echo -e "\e[1;32mOK\e[0m" else echo -e "\e[1;31mFAIL: response does not match!" jd -color "$EXPECTED_RESPONSE_FILE" <(printf "%s" "$sorted_response") exit 1 fi