diff --git a/scripts/generate-configs.sh b/scripts/generate-configs.sh index a56387d..fdebee8 100644 --- a/scripts/generate-configs.sh +++ b/scripts/generate-configs.sh @@ -25,8 +25,9 @@ log() { printf '%s\n' "$*"; } dbg() { if [ "$DEBUG" != "0" ]; then printf '[DEBUG] %s\n' "$*" | tee -a "$DEBUG_LOG"; fi } err() { printf '[ERROR] %s\n' "$*" | tee -a "$DEBUG_LOG" >&2; } -if ! command -v curl >/dev/null 2>&1; then err "curl is required"; exit 2; fi -if ! command -v jq >/dev/null 2>&1; then err "jq is required"; exit 2; fi +for cmd in curl jq; do + if ! command -v "$cmd" >/dev/null 2>&1; then err "$cmd is required"; exit 2; fi +done if [ "$DEBUG" != "0" ]; then : > "$DEBUG_LOG"; dbg "Debugging enabled"; fi @@ -68,37 +69,35 @@ query_api() { local max_attempts=5 local attempt=0 local max_sleep=8 + local tmpfile body http_code sleep_time while :; do attempt=$((attempt+1)) dbg " -> API attempt #$attempt for $domain" - raw="$(timeout 20 curl -sS --compressed \ - -m 10 --connect-timeout 5 \ - -H 'Accept: application/json' \ - -w '\n%{http_code}\n%{content_type}' \ - "${API_URL}${domain}" 2>>"$DEBUG_LOG" || true)" + tmpfile=$(mktemp) + # curl с отдельным файлом для тела, HTTP-код через -w + http_code=$(curl -sS --compressed \ + -m 10 --connect-timeout 5 \ + -H 'Accept: application/json' \ + -w '%{http_code}' \ + -o "$tmpfile" \ + "${API_URL}${domain}" 2>>"$DEBUG_LOG" || true) - # корректно разделяем тело и метаданные - http_code="$(printf '%s' "$raw" | tail -n1)" - content_type="$(printf '%s' "$raw" | tail -n2 | head -n1)" - body="$(printf '%s' "$raw" | head -n -2)" # тело без последних двух строк - - # убираем возможные пустые строки - body="$(printf '%s' "$body" | sed '/^[[:space:]]*$/d')" + body=$(cat "$tmpfile") + rm -f "$tmpfile" preview="$(printf '%s' "$body" | tr '\n' ' ' | cut -c1-400)" - dbg " -> HTTP=${http_code}, Content-Type=${content_type}, preview=${preview}" + dbg " -> HTTP=$http_code, preview=${preview}" - # пустой body — retry - if [ -n "$body" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then + if [ "$http_code" = "200" ] && jq -e . >/dev/null 2>&1 <<<"$body"; then echo "$body" return 0 fi if [ "$attempt" -ge "$max_attempts" ]; then - ERRORS["$domain"]="http_${http_code}_or_nonjson" - dbg " -> Failed after $attempt attempts: HTTP=${http_code}, preview=${preview}" + ERRORS["$domain"]="http_${http_code}_or_invalid_json" + dbg " -> Failed after $attempt attempts, preview=${preview}" return 1 fi @@ -109,7 +108,6 @@ query_api() { done } - if [ ! -f "$INPUT_FILE" ]; then err "Input file not found: $INPUT_FILE" exit 3 @@ -119,7 +117,7 @@ raw_total_lines=$(wc -l < "$INPUT_FILE" | tr -d ' ') dbg "Raw input lines: $raw_total_lines" lineno=0 -while IFS= read -r line; do +while IFS= read -r line || [ -n "$line" ]; do lineno=$((lineno+1)) total_lines=$((total_lines+1)) dbg "Processing line #$lineno: '$line'"