Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created May 9, 2024 19:54
Show Gist options
  • Save pythoninthegrass/5d4020574e520adef6483291d16a6b63 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/5d4020574e520adef6483291d16a6b63 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
# get the root directory
GIT_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
if [ -n "$GIT_ROOT" ]; then
TLD="$GIT_ROOT"
else
TLD="${SCRIPT_DIR}"
fi
ENV_FILE="${TLD}/.env"
# env vars
[[ -f "${ENV_FILE}" ]] && export $(grep -v '^#' ${ENV_FILE} | xargs)
BASE_URL="https://api.godaddy.com/v1/"
# check if jq is installed
[[ $(command -v jq 2&>/dev/null; echo $?) -ne 0 ]] && echo "jq is not installed" && exit 1
# Authenticate
auth() {
curl -X GET -H "Authorization: sso-key $API_KEY:$API_SECRET" "$1"
}
# Check if domain is available
get_domains_available() {
# /domains/available?domain=example.guru"
auth "$BASE_URL/domains/available?domain=$DOMAIN" | jq
}
# Get subscriptions
get_subscriptions() {
# /subscriptions
auth "$BASE_URL/subscriptions" | jq
}
# Get records
get_records() {
# /domains/{domain}/records/{type}/{name}
type="$1"
name="$2"
desc=$(cat <<-EOF
types: A, AAAA, CNAME, MX, NS, SOA, SRV, TXT
names: @, www, subdomain
get_records $type $name
EOF
)
[[ -z $type || -z $name ]] && echo "$desc" && exit 1
auth "$BASE_URL/domains/$DOMAIN/records/$type/$name" | jq
}
# TODO: check godaddy api for the correct endpoint
# * https://developer.godaddy.com/doc/endpoint/domains
# Delete records
delete_records() {
# /domains/{domain}/records/{type}/{name}
type="$1"
name="$2"
desc=$(cat <<-EOF
types: A, AAAA, CNAME, MX, NS, SOA, SRV, TXT
names: @, www, subdomain
delete_records $type $name
EOF
)
[[ -z $type || -z $name ]] && echo "$desc" && exit 1
echo "Deleting records for $DOMAIN"
echo "Type: $type"
echo "Name: $name"
read -rp "Are you sure you want to delete the records? (y/n): " confirm
case $confirm in
[Yy]|[Yy][Ee][Ss])
# auth -X DELETE "$BASE_URL/domains/$DOMAIN/records/$type/$name" | jq
echo "Would have deleted records:"
get_records $type $name
;;
*)
echo "Deletion cancelled."
;;
esac
}
main() {
# get_domains_available
# get_subscriptions
# get_records A @
delete_records A @
}
main "$@"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment