resolveLatest follows the /releases/latest redirect to learn the tag without needing the GitHub API (which is rate-limited for unauth users).
()
| 121 | // resolveLatest follows the /releases/latest redirect to learn the tag |
| 122 | // without needing the GitHub API (which is rate-limited for unauth users). |
| 123 | func resolveLatest() (string, error) { |
| 124 | client := &http.Client{ |
| 125 | Timeout: 10 * time.Second, |
| 126 | CheckRedirect: func(r *http.Request, via []*http.Request) error { |
| 127 | return http.ErrUseLastResponse |
| 128 | }, |
| 129 | } |
| 130 | resp, err := client.Get("https://github.com/SamNet-dev/snix/releases/latest") |
| 131 | if err != nil { |
| 132 | return "", err |
| 133 | } |
| 134 | defer resp.Body.Close() |
| 135 | loc := resp.Header.Get("Location") |
| 136 | if loc == "" { |
| 137 | // Some proxies strip redirects; fall back to the JSON API. |
| 138 | return resolveLatestAPI() |
| 139 | } |
| 140 | // Expect: https://github.com/SamNet-dev/snix/releases/tag/v0.5.0 |
| 141 | idx := strings.LastIndex(loc, "/v") |
| 142 | if idx < 0 { |
| 143 | return "", fmt.Errorf("unexpected redirect: %s", loc) |
| 144 | } |
| 145 | return loc[idx+2:], nil |
| 146 | } |
| 147 | |
| 148 | func resolveLatestAPI() (string, error) { |
| 149 | resp, err := http.Get("https://api.github.com/repos/SamNet-dev/snix/releases/latest") |
no test coverage detected