~craftyguy/ddns.sh

ad5c0d2ce1f6cb3288ec040ce75e4b280359bdcd — Clayton Craft 7 months ago a9ffc08
Support standard jq, but use gojq if available
1 files changed, 8 insertions(+), 4 deletions(-)

M ddns.sh
M ddns.sh => ddns.sh +8 -4
@@ 2,6 2,10 @@
#
set -euo pipefail

_jq=jq
# gojq is sooo much faster than jq, so prefer it if it is available
[ command -v gojq 2>/dev/null ] && _jq=gojq

usage() {
	cat <<-EOF
		Usage: $0 <path to configuration>


@@ 48,14 52,14 @@ luadns() {
	_zones="$(luadns_get zones)"

	local _zone_cnt
	_zone_cnt="$(echo "$_zones" | gojq '. | length')"
	_zone_cnt="$(echo "$_zones" | "$_jq" '. | length')"
	if [[ "$_zone_cnt" -eq 0 ]]; then
		return
	fi

	local _zone_id
	# shellcheck disable=SC2016
	_zone_id="$(echo "$_zones" | gojq --arg domain "$DOMAIN" ' .[] | select(.name==$domain) | .["id"] ' )"
	_zone_id="$(echo "$_zones" | "$_jq" --arg domain "$DOMAIN" ' .[] | select(.name==$domain) | .["id"] ' )"
	[ -n "$_zone_id" ] || fail "error: unable to get zone ID for $DOMAIN"

	local _zone


@@ 63,11 67,11 @@ luadns() {
	[ -n "$_zone" ] || fail "error: unable to get zone info for $DOMAIN"

	local _dns_ip
	_dns_ip="$(echo "$_zone" | gojq '.["records"] | .[] | select(.type=="A") | .["content"] ' | sed 's|"||g' )"
	_dns_ip="$(echo "$_zone" | "$_jq" '.["records"] | .[] | select(.type=="A") | .["content"] ' | sed 's|"||g' )"
	[ -n "$_dns_ip" ] || fail "error: unable to determine DNS IP for $DOMAIN"

	local _record_id
	_record_id="$(echo "$_zone" | gojq '.["records"] | .[] | select(.type=="A") | .["id"] ' )"
	_record_id="$(echo "$_zone" | "$_jq" '.["records"] | .[] | select(.type=="A") | .["id"] ' )"
	[ -n "$_record_id" ] || fail "error: unable to determine record ID"

	if [ "$_dns_ip" != "$IP_ADDRESS" ]; then