summaryrefslogtreecommitdiff
path: root/scripts/update-metadata.sh
blob: f9ab3636ed473137578914cb0ab4f80384d337c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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"