#!/bin/bash # TODO: Detect whenever we can use HTTPie in the shell environment or use plain old curl. HTTP_CLIENT_PATH=$(command -v curl) if [[ $HTTP_CLIENT_PATH == "" ]]; then echo "Please install cURL first as this script may not work in wGET." exit 1 fi if command -v jq >> /dev/null; then true else echo "Please install jq to prettify JSON responses from API." exit 1 fi ### COLORS ### RED=$(printf '\033[31m') GREEN=$(printf '\033[32m') YELLOW=$(printf '\033[33m') BLUE=$(printf '\033[34m') MAGENTA=$(printf '\033[35m') BOLD=$(printf '\033[1m') RESET=$(printf '\033[m') ### COLORS ### warn() { if [[ $NO_COLOR == "" ]]; then echo "${YELLOW}warning: $* ${RESET}" else echo "warninh: $*" fi } error() { echo "${RED}error: $* ${RESET}" } info() { echo "info: $*" } # API Handler apiHandler() { CF_HOST="https://api.cloudflare.com/client/v4" RESPONSE_FORMAT="application/json" API_METHOD=${2:GET} curl -H "Content-Type:$RESPONSE_FORMAT" \ -H "Authorization: Bearer $CF_API_KEY" \ -X "$API_METHOD" "$CF_HOST/$1" } if [[ $# == "0" ]] || [[ $1 == "help" ]]; then echo "Usage: $0 COMMAND ARGS" echo "Available commands:" echo "validate-token Check if an token is still working" echo "zones Manage your Cloudflare zones" else if [[ $1 == "validate-token" ]]; then if [[ $CF_API_EMAIL != "" ]]; then echo "Unsupported variable found: CF_API_EMAIL" echo "Please use API keys instead of global API token." exit 1 fi if [[ $CF_API_KEY != "" ]]; then apiHandler user/tokens/verify GET | jq else echo "No token found! Set your API token with:" echo " export CF_API_KEY=abcdef123456" fi elif [[ $1 == "zones" ]]; then echo "Usage: $0 zones [ARGS]" echo echo "Manage and list your zones managed by Cloudflare." echo "Available arguments:" echo "list List available zones to manage" echo "add Add new zone" echo "remove Remove an zone" exit elif [[ $1 == "zones" && $2 == "list" ]]; then apiHandler zones GET | jq .result else echo "Unsupported command! See the help command for details" fi fi