|
4 | 4 | "encoding/json"
|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
| 7 | + "io" |
7 | 8 | "net/http"
|
8 | 9 | "strings"
|
9 | 10 |
|
@@ -51,19 +52,35 @@ func GithubTagFromVersion(owner, repo, version string) (string, error) {
|
51 | 52 | }
|
52 | 53 |
|
53 | 54 | func LibraryVersionFromCGVersion(owner, repo, cgVersion string) string {
|
54 |
| - res, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/main/versions.json", owner, repo)) |
55 |
| - if err != nil || res.StatusCode != http.StatusOK { |
| 55 | + body, err := LoadVersionsJSON(owner, repo) |
| 56 | + if err != nil { |
56 | 57 | cli.Warn("Couldn't fetch versions.json. Using latest client library version.")
|
57 | 58 | return "latest"
|
58 | 59 | }
|
59 |
| - defer res.Body.Close() |
| 60 | + defer body.Close() |
60 | 61 |
|
61 | 62 | var versions map[string]string
|
62 |
| - err = json.NewDecoder(res.Body).Decode(&versions) |
| 63 | + err = json.NewDecoder(body).Decode(&versions) |
63 | 64 | if err != nil {
|
64 | 65 | cli.Warn("Invalid versions.json. Using latest client library version.")
|
65 | 66 | return "latest"
|
66 | 67 | }
|
67 | 68 |
|
68 | 69 | return CompatibleVersion(versions, cgVersion)
|
69 | 70 | }
|
| 71 | + |
| 72 | +func LoadVersionsJSON(owner, repo string) (io.ReadCloser, error) { |
| 73 | + res, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/main/versions.json", owner, repo)) |
| 74 | + if res.StatusCode != http.StatusOK { |
| 75 | + err = errors.New("invalid http status") |
| 76 | + } |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + res, err = http.Get(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/master/versions.json", owner, repo)) |
| 80 | + if res.StatusCode != http.StatusOK { |
| 81 | + err = errors.New("invalid http status") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return res.Body, err |
| 86 | +} |
0 commit comments