infra-role-nimbus-fluffy/templates/rpc.sh.j2

34 lines
673 B
Django/Jinja

#!/usr/bin/env bash
# vim: set ft=sh:
set -euo pipefail
URL="http://localhost:{{ nimbus_fluffy_rpc_port }}/"
METHOD="$1"
shift
if [[ -z "${METHOD}" ]]; then
echo "No method specified!" >&2
exit 1
fi
if [[ -n "${@}" ]]; then
PARAMS=$(printf '%s,' "${@}")
PARAMS="${@%%,}\""
else
PARAMS=''
fi
PAYLOAD="{
\"id\": 1,
\"jsonrpc\": \"2.0\",
\"method\": \"${METHOD}\",
\"params\": [${PARAMS}]
}"
# The jq script checks if error exists and adjusts exit code.
curl -sSf -X POST --max-time 5 \
-H "Content-type:application/json" \
--data "${PAYLOAD}" \
"${URL}" | \
jq -e '., if .error != null then null|halt_error(2) else halt end'