FetchGoVersion fetches the Go version string from the given URL. Returns ErrBranchNotFound if the release branch does not exist (HTTP 404).
(ctx context.Context, url string)
| 114 | // FetchGoVersion fetches the Go version string from the given URL. |
| 115 | // Returns ErrBranchNotFound if the release branch does not exist (HTTP 404). |
| 116 | func FetchGoVersion(ctx context.Context, url string) (string, error) { |
| 117 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) |
| 118 | if err != nil { |
| 119 | return "", fmt.Errorf("creating request: %w", err) |
| 120 | } |
| 121 | |
| 122 | resp, err := http.DefaultClient.Do(req) |
| 123 | if err != nil { |
| 124 | return "", fmt.Errorf("fetching Go version: %w", err) |
| 125 | } |
| 126 | |
| 127 | defer func() { _ = resp.Body.Close() }() |
| 128 | |
| 129 | if resp.StatusCode == http.StatusNotFound { |
| 130 | return "", fmt.Errorf("%w: %s", ErrBranchNotFound, url) |
| 131 | } |
| 132 | |
| 133 | if resp.StatusCode != http.StatusOK { |
| 134 | return "", fmt.Errorf("%w: %d", ErrHTTPStatus, resp.StatusCode) |
| 135 | } |
| 136 | |
| 137 | body, err := io.ReadAll(resp.Body) |
| 138 | if err != nil { |
| 139 | return "", fmt.Errorf("reading Go version response: %w", err) |
| 140 | } |
| 141 | |
| 142 | return strings.TrimSpace(string(body)), nil |
| 143 | } |