#! /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 REBUILD_OP="${1:?nixos-rebuild related API operation not given error, e.g. Rebuild, Rollback, Upgrade}" readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN:?not set error}" QUERY=$(cat << EOF mutation RebuildSwitch { __typename system { runSystem${REBUILD_OP} { code success } } } EOF ) # shellcheck disable=SC2086,SC2116 REQUEST=$(cat << EOF { "query": "$(echo $QUERY)", "variables": null } EOF ) readonly REQUEST EXPECTED_RESPONSE=$(cat << EOF { "data": { "__typename": "Mutation", "system": { "runSystem${REBUILD_OP}": { "code": 200, "success": true } } } } EOF ) 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 <(printf "%s\n" "$EXPECTED_RESPONSE") <(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 <(printf "%s\n" "$EXPECTED_RESPONSE") <(printf "%s" "$sorted_response") exit 1 fi