#!/usr/bin/env bash set -euo pipefail BASE_URL="${BASE_URL:-https://api.alibabacloud.com}" LANGUAGE="${LANGUAGE:-EN_US}" BUILD_DIR="${BUILD_DIR:-./build}" CONCURRENCY=${CONCURRENCY:-4} NORMALIZED_LANGUAGE=$(echo "${LANGUAGE}" | tr "[:upper:]" "[:lower:]") TARGET_DIR="${BUILD_DIR%/}/${NORMALIZED_LANGUAGE}" PRODUCTS_FILE="${TARGET_DIR}/products.json" # Let's get started echo "[-] Update metadata with language ${LANGUAGE}" # Reset the directory for legacy product removal mkdir -p "${BUILD_DIR}" rm -rf "${TARGET_DIR}" && mkdir -p "${TARGET_DIR}" # Update product list echo "[-] Updating product list" curl -sSf "${BASE_URL}/meta/v1/products.json?language=${LANGUAGE}" -o "$PRODUCTS_FILE" # Update all available APIs counter=0 while read -r product; do # Extract the product code code=$(jq -r '.code' <<< "$product") code_lowercase=$(tr "[:upper:]" "[:lower:]" <<< "$code") # Loop through each version for the current product while read -r version; do api_docs_url="${BASE_URL}/meta/v1/products/${code}/versions/${version}/api-docs.json?language=${LANGUAGE}" version_dir="${TARGET_DIR}/${code_lowercase}/${version}" api_docs_path="${version_dir}/api-docs.json" # Create the directory if it doesn't exist mkdir -p "$version_dir" echo "[-] Updating ${code} ${version}" curl -sSf "${api_docs_url}" -o "$api_docs_path" & # Increment the counter ((++counter)) # If the counter reaches the maximum number of jobs, wait for them # to finish before continuing if ((counter >= CONCURRENCY)); then wait counter=0 fi done < <(jq -r '.versions[]' <<< "$product") done < <(jq -c '.[]' "$PRODUCTS_FILE") # Wait for all background processes to finish before exiting wait echo "[-] Update done"