Skip to content

Commit 7081950

Browse files
committed
Try master branch if main branch doesn't exist
1 parent b430a56 commit 7081950

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

util/github.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
78
"net/http"
89
"strings"
910

@@ -51,19 +52,35 @@ func GithubTagFromVersion(owner, repo, version string) (string, error) {
5152
}
5253

5354
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 {
5657
cli.Warn("Couldn't fetch versions.json. Using latest client library version.")
5758
return "latest"
5859
}
59-
defer res.Body.Close()
60+
defer body.Close()
6061

6162
var versions map[string]string
62-
err = json.NewDecoder(res.Body).Decode(&versions)
63+
err = json.NewDecoder(body).Decode(&versions)
6364
if err != nil {
6465
cli.Warn("Invalid versions.json. Using latest client library version.")
6566
return "latest"
6667
}
6768

6869
return CompatibleVersion(versions, cgVersion)
6970
}
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+
}

util/modules.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ func findModuleVersion(name, libraryVersion, projectType string) (string, error)
4848
return strings.TrimPrefix(version, "v"), err
4949
}
5050

51-
res, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/code-game-project/codegame-cli-%s/main/versions.json", name))
52-
if err != nil || res.StatusCode != http.StatusOK {
53-
cli.Warn("Couldn't fetch versions.json. Using latest go module version.")
51+
body, err := LoadVersionsJSON("code-game-project", "codegame-cli-"+name)
52+
if err != nil {
53+
cli.Warn("Couldn't fetch versions.json. Using latest %s module version.", name)
5454
version, err := LatestGithubTag("code-game-project", "codegame-cli-"+name)
5555
return strings.TrimPrefix(version, "v"), err
5656
}
57-
defer res.Body.Close()
57+
defer body.Close()
5858

5959
type jsonObj struct {
6060
Server map[string]string
6161
Client map[string]string
6262
}
6363

6464
var versions jsonObj
65-
err = json.NewDecoder(res.Body).Decode(&versions)
65+
err = json.NewDecoder(body).Decode(&versions)
6666
if err != nil {
67-
cli.Warn("Invalid versions.json. Using latest go module version.")
67+
cli.Warn("Invalid versions.json. Using latest %s module version.", name)
6868
version, err := LatestGithubTag("code-game-project", "codegame-cli-"+name)
6969
return strings.TrimPrefix(version, "v"), err
7070
}

0 commit comments

Comments
 (0)