#!/usr/bin/env bash set -euo pipefail env_name="${TOPIC_ENV:-${ENVIRONMENT:-stage}}" repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" output_path="${TOPIC_MANIFEST_LOCAL_PATH:-${repo_root}/.generated/kafka-topics/${env_name}/manifest.yaml}" mkdir -p "$(dirname "${output_path}")" if [[ -z "${TOPIC_MANIFEST_PATH:-}" ]]; then echo "TOPIC_MANIFEST_PATH is required for kafka-topics component" >&2 exit 1 fi if [[ -z "${TOPIC_MANIFEST_REPO:-}" ]]; then if [[ -f "${TOPIC_MANIFEST_PATH}" ]]; then cp "${TOPIC_MANIFEST_PATH}" "${output_path}" echo "Copied local topic manifest to ${output_path}" exit 0 fi echo "TOPIC_MANIFEST_REPO is required when ${TOPIC_MANIFEST_PATH} is not local" >&2 exit 1 fi if [[ -z "${TOPIC_MANIFEST_SHA:-}" ]]; then echo "TOPIC_MANIFEST_SHA is required" >&2 exit 1 fi server_url="${CI_SERVER_URL:-https://gitlab.sarex.io}" encoded_path="${TOPIC_MANIFEST_PATH#/}" urlencode() { python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1], safe=""))' "$1" } encoded_project="$(urlencode "${TOPIC_MANIFEST_REPO}")" encoded_file="$(urlencode "${encoded_path}")" encoded_ref="$(urlencode "${TOPIC_MANIFEST_SHA}")" manifest_url="${server_url}/api/v4/projects/${encoded_project}/repository/files/${encoded_file}/raw?ref=${encoded_ref}" curl_args=(-fsSL) if [[ -n "${CI_JOB_TOKEN:-}" ]]; then curl_args+=(-H "JOB-TOKEN: ${CI_JOB_TOKEN}") fi curl "${curl_args[@]}" "${manifest_url}" -o "${output_path}" if grep -qiE '^\s*&2 exit 1 fi echo "Downloaded topic manifest to ${output_path}"