|
3 | 3 | # Script to download and install gh-aw binary for the current OS and architecture |
4 | 4 | # Supports: Linux, macOS (Darwin), FreeBSD, Windows (Git Bash/MSYS/Cygwin) |
5 | 5 | # Usage: ./install-gh-aw.sh [version] |
6 | | -# If no version is specified, it will use the latest release |
| 6 | +# If no version is specified, it will fetch and use the latest release |
| 7 | +# Example: ./install-gh-aw.sh v1.0.0 |
7 | 8 |
|
8 | 9 | set -e # Exit on any error |
9 | 10 |
|
@@ -109,48 +110,72 @@ print_info "Detected OS: $OS -> $OS_NAME" |
109 | 110 | print_info "Detected architecture: $ARCH -> $ARCH_NAME" |
110 | 111 | print_info "Platform: $PLATFORM" |
111 | 112 |
|
112 | | -# Function to fetch release data with fallback for invalid token |
| 113 | +# Function to fetch release data with fallback for invalid token and retry logic |
113 | 114 | fetch_release_data() { |
114 | 115 | local url=$1 |
| 116 | + local max_retries=3 |
| 117 | + local retry_delay=2 |
115 | 118 | local use_auth=false |
116 | | - local curl_args=("-s" "-f") |
117 | 119 |
|
118 | 120 | # Try with authentication if GH_TOKEN is set |
119 | 121 | if [ -n "$GH_TOKEN" ]; then |
120 | 122 | use_auth=true |
121 | | - curl_args+=("-H" "Authorization: Bearer $GH_TOKEN") |
122 | 123 | fi |
123 | 124 |
|
124 | | - # Make the API call |
125 | | - local response |
126 | | - response=$(curl "${curl_args[@]}" "$url" 2>/dev/null) |
127 | | - local exit_code=$? |
128 | | - |
129 | | - # If the call failed and we used authentication, try again without it |
130 | | - if [ $exit_code -ne 0 ] && [ "$use_auth" = true ]; then |
131 | | - print_warning "API call with GH_TOKEN failed. Retrying without authentication..." |
132 | | - print_warning "Your GH_TOKEN may be incompatible (typically SSO) with this request." |
133 | | - response=$(curl -s -f "$url" 2>/dev/null) |
134 | | - exit_code=$? |
135 | | - fi |
136 | | - |
137 | | - if [ $exit_code -ne 0 ]; then |
138 | | - return 1 |
139 | | - fi |
| 125 | + # Retry loop |
| 126 | + for attempt in $(seq 1 $max_retries); do |
| 127 | + local curl_args=("-s" "-f") |
| 128 | + |
| 129 | + # Add auth header if using authentication |
| 130 | + if [ "$use_auth" = true ]; then |
| 131 | + curl_args+=("-H" "Authorization: Bearer $GH_TOKEN") |
| 132 | + fi |
| 133 | + |
| 134 | + print_info "Fetching release data (attempt $attempt/$max_retries)..." >&2 |
| 135 | + |
| 136 | + # Make the API call |
| 137 | + local response |
| 138 | + response=$(curl "${curl_args[@]}" "$url" 2>/dev/null) |
| 139 | + local exit_code=$? |
| 140 | + |
| 141 | + # Success |
| 142 | + if [ $exit_code -eq 0 ] && [ -n "$response" ]; then |
| 143 | + echo "$response" |
| 144 | + return 0 |
| 145 | + fi |
| 146 | + |
| 147 | + # If this was the first attempt with auth and it failed, try without auth |
| 148 | + if [ "$attempt" -eq 1 ] && [ "$use_auth" = true ]; then |
| 149 | + print_warning "API call with GH_TOKEN failed. Retrying without authentication..." >&2 |
| 150 | + print_warning "Your GH_TOKEN may be incompatible (typically SSO) with this request." >&2 |
| 151 | + use_auth=false |
| 152 | + # Don't count this as a retry attempt, just switch auth mode |
| 153 | + continue |
| 154 | + fi |
| 155 | + |
| 156 | + # If we haven't exhausted retries, wait and try again |
| 157 | + if [ "$attempt" -lt "$max_retries" ]; then |
| 158 | + print_warning "Fetch attempt $attempt failed (exit code: $exit_code). Retrying in ${retry_delay}s..." >&2 |
| 159 | + sleep $retry_delay |
| 160 | + retry_delay=$((retry_delay * 2)) |
| 161 | + else |
| 162 | + print_error "Failed to fetch release data after $max_retries attempts" >&2 |
| 163 | + fi |
| 164 | + done |
140 | 165 |
|
141 | | - echo "$response" |
142 | | - return 0 |
| 166 | + return 1 |
143 | 167 | } |
144 | 168 |
|
145 | 169 | # Get version (use provided version or fetch latest) |
146 | 170 | VERSION=${1:-""} |
147 | 171 | REPO="githubnext/gh-aw" |
148 | 172 |
|
149 | 173 | if [ -z "$VERSION" ]; then |
150 | | - print_info "Fetching latest release information..." |
| 174 | + print_info "No version specified, fetching latest release information from GitHub..." |
151 | 175 |
|
152 | 176 | if ! LATEST_RELEASE=$(fetch_release_data "https://api.github.com/repos/$REPO/releases/latest"); then |
153 | | - print_error "Failed to fetch latest release information" |
| 177 | + print_error "Failed to fetch latest release information from GitHub API" |
| 178 | + print_info "You can specify a version directly: ./install-gh-aw.sh v1.0.0" |
154 | 179 | exit 1 |
155 | 180 | fi |
156 | 181 |
|
@@ -206,7 +231,7 @@ for attempt in $(seq 1 $MAX_RETRIES); do |
206 | 231 | print_success "Binary downloaded successfully" |
207 | 232 | break |
208 | 233 | else |
209 | | - if [ $attempt -eq $MAX_RETRIES ]; then |
| 234 | + if [ "$attempt" -eq "$MAX_RETRIES" ]; then |
210 | 235 | print_error "Failed to download binary from $DOWNLOAD_URL after $MAX_RETRIES attempts" |
211 | 236 | print_info "Please check if the version and platform combination exists in the releases." |
212 | 237 | exit 1 |
|
0 commit comments