|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Downloads the Contentstack regions registry from the official source and |
| 4 | +# saves it to src/main/resources/regions.json. |
| 5 | +# |
| 6 | +# Invoked automatically by Maven on the generate-resources phase, and |
| 7 | +# manually via: bash scripts/download-regions.sh |
| 8 | +# |
| 9 | +# Requires: curl (preferred) or wget as fallback |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +URL="https://artifacts.contentstack.com/regions.json" |
| 14 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 15 | +DEST="${SCRIPT_DIR}/../src/main/resources/regions.json" |
| 16 | +DIR="$(dirname "$DEST")" |
| 17 | + |
| 18 | +mkdir -p "$DIR" |
| 19 | + |
| 20 | +data="" |
| 21 | + |
| 22 | +# --- Attempt 1: curl (preferred) -------------------------------------------- |
| 23 | +if command -v curl &>/dev/null; then |
| 24 | + data=$(curl --silent --fail --location --max-time 30 "$URL") || data="" |
| 25 | +fi |
| 26 | + |
| 27 | +# --- Attempt 2: wget fallback ----------------------------------------------- |
| 28 | +if [[ -z "$data" ]] && command -v wget &>/dev/null; then |
| 29 | + data=$(wget --quiet --timeout=30 -O - "$URL") || data="" |
| 30 | +fi |
| 31 | + |
| 32 | +# --- Validate and write ------------------------------------------------------ |
| 33 | +if [[ -z "$data" ]]; then |
| 34 | + echo "contentstack/cms: Warning — could not download regions.json." >&2 |
| 35 | + echo " The SDK will attempt to download it at runtime on first use." >&2 |
| 36 | + exit 0 # non-fatal: runtime fallback in Endpoint.java handles it |
| 37 | +fi |
| 38 | + |
| 39 | +# Basic validation: must contain a "regions" key |
| 40 | +if ! echo "$data" | grep -q '"regions"'; then |
| 41 | + echo "contentstack/cms: Warning — downloaded data is not valid regions.json." >&2 |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | + |
| 45 | +echo "$data" > "$DEST" |
| 46 | + |
| 47 | +region_count=$(echo "$data" | grep -o '"id"' | wc -l | tr -d ' ') |
| 48 | +echo "contentstack/cms: regions.json downloaded (${region_count} regions)." |
0 commit comments