|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Base URL for the crates.io API |
| 4 | +CRATES_IO_API_BASE="https://crates.io/api/v1" |
| 5 | +# Delay between API requests to be polite (in seconds) |
| 6 | +REQUEST_DELAY=0.2 |
| 7 | + |
| 8 | +# Function to fetch JSON from a URL and handle basic errors |
| 9 | +# Arguments: |
| 10 | +# $1: URL to fetch |
| 11 | +# Returns: |
| 12 | +# JSON string on success, empty string on failure |
| 13 | +# Returns 0 on success, 1 on curl/network failure, 0 for 404 (handled case) |
| 14 | +fetch_json() { |
| 15 | + local url="$1" |
| 16 | + local response_and_status |
| 17 | + local http_status |
| 18 | + local response_body |
| 19 | + |
| 20 | + # Use -s for silent, --max-time for timeout |
| 21 | + # -w "\n%{http_code}" appends the HTTP status code on a new line to stdout. |
| 22 | + # This helps in reliably separating the body from the status code. |
| 23 | + response_and_status=$(curl -s --max-time 10 -w "\n%{http_code}" "$url" 2>/dev/null) |
| 24 | + local curl_exit=$? |
| 25 | + |
| 26 | + # Check if curl command itself failed (e.g., network error, timeout before HTTP response) |
| 27 | + if [[ $curl_exit -ne 0 ]]; then |
| 28 | + echo "Error: curl command failed for $url" >&2 |
| 29 | + return 1 # Indicate failure |
| 30 | + fi |
| 31 | + |
| 32 | + # Ensure we have some output to parse |
| 33 | + if [[ -z "$response_and_status" ]]; then |
| 34 | + echo "Error: curl command failed for $url" >&2 |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + |
| 38 | + # Split the response into body and status code based on the newline added by -w |
| 39 | + # The last line is the status code, everything before it is the body. |
| 40 | + http_status=$(echo -n "$response_and_status" | tail -n 1) |
| 41 | + response_body=$(echo -n "$response_and_status" | head -n -1) |
| 42 | + |
| 43 | + # Handle 2xx success, 404 as handled empty, and other statuses as errors. |
| 44 | + if [[ "$http_status" =~ ^2[0-9][0-9]$ ]]; then |
| 45 | + echo "$response_body" |
| 46 | + sleep "$REQUEST_DELAY" # Be polite |
| 47 | + return 0 |
| 48 | + elif [[ "$http_status" == "404" ]]; then |
| 49 | + # Crate or version not found. This is a handled case, not necessarily an error for the script's logic. |
| 50 | + echo "" |
| 51 | + sleep "$REQUEST_DELAY" |
| 52 | + return 0 |
| 53 | + else |
| 54 | + echo "Error: Failed to fetch $url. HTTP Status: $http_status. Response: ${response_body:0:100}..." >&2 |
| 55 | + return 1 # Indicate failure |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Only run main when executed directly, not when sourced by tests |
| 60 | +if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
| 61 | + # Main script logic |
| 62 | + if [[ "\$#" -eq 0 ]]; then |
| 63 | + echo "Usage: $0 <crate_name_1> [crate_name_2] ..." >&2 |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + for crate_name in "\$@"; do |
| 68 | + # 1. Try to get max_version from /api/v1/crates/{name} |
| 69 | + CRATE_INFO_URL="${CRATES_IO_API_BASE}/crates/${crate_name}" |
| 70 | + crate_info_json=$(fetch_json "$CRATE_INFO_URL") |
| 71 | + |
| 72 | + if [[ $? -ne 0 ]]; then # Check if fetch_json itself failed (e.g., curl error) |
| 73 | + echo "Skipping '$crate_name': Failed to retrieve crate info due to network/curl error." >&2 |
| 74 | + continue |
| 75 | + fi |
| 76 | + |
| 77 | + target_version="" |
| 78 | + if [[ -n "$crate_info_json" ]]; then |
| 79 | + max_version=$(echo "$crate_info_json" | jq -r '.crate.max_version // empty') |
| 80 | + if [[ -n "$max_version" ]]; then |
| 81 | + target_version="$max_version" |
| 82 | + fi |
| 83 | + fi |
| 84 | + |
| 85 | + if [[ -z "$target_version" ]]; then |
| 86 | + # 2. If max_version is empty or not found, fetch all versions and pick the first non-yanked one |
| 87 | + VERSIONS_URL="${CRATES_IO_API_BASE}/crates/${crate_name}/versions" |
| 88 | + versions_json=$(fetch_json "$VERSIONS_URL") |
| 89 | + |
| 90 | + if [[ $? -ne 0 ]]; then |
| 91 | + echo "Skipping '$crate_name': Failed to retrieve versions list due to network/curl error." >&2 |
| 92 | + continue |
| 93 | + fi |
| 94 | + |
| 95 | + if [[ -n "$versions_json" ]]; then |
| 96 | + # Find the first non-yanked version number |
| 97 | + target_version=$(echo "$versions_json" | jq -r '.versions[] | select(.yanked == false) | .num' | head -n 1) |
| 98 | + fi |
| 99 | + |
| 100 | + if [[ -z "$target_version" ]]; then |
| 101 | + echo "Skipping '$crate_name': No non-yanked versions found or could not retrieve versions." >&2 |
| 102 | + continue |
| 103 | + fi |
| 104 | + fi |
| 105 | + |
| 106 | + # 3. Now that we have a target_version, fetch its metadata and check features |
| 107 | + VERSION_METADATA_URL="${CRATES_IO_API_BASE}/crates/${crate_name}/${target_version}" |
| 108 | + version_metadata_json=$(fetch_json "$VERSION_METADATA_URL") |
| 109 | + |
| 110 | + if [[ $? -ne 0 ]]; then |
| 111 | + echo "Skipping '$crate_name': Failed to retrieve metadata for version '$target_version' due to network/curl error." >&2 |
| 112 | + continue |
| 113 | + fi |
| 114 | + |
| 115 | + if [[ -z "$version_metadata_json" ]]; then |
| 116 | + echo "Skipping '$crate_name': Could not retrieve metadata for version '$target_version' (empty response)." >&2 |
| 117 | + continue |
| 118 | + fi |
| 119 | + |
| 120 | + # Check if the features map is non-empty |
| 121 | + feature_count=$(echo "$version_metadata_json" | jq -r '.version.features | to_entries | length // 0') |
| 122 | + |
| 123 | + if [[ "$feature_count" -gt 0 ]]; then |
| 124 | + echo "$crate_name" |
| 125 | + fi |
| 126 | + done |
| 127 | +fi |
0 commit comments