Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: processing invalid GITHUB_TOKEN #140

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 45 additions & 39 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type GoBrew struct {
}

var gb GoBrew
var githubTags map[string][]string

// NewGoBrew instance
func NewGoBrew() GoBrew {
Expand Down Expand Up @@ -185,7 +184,7 @@ func (gb *GoBrew) ListVersions() {
// ListRemoteVersions that are installed by dir ls
func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string {
color.Infoln("==> [Info] Fetching remote versions\n")
tags := gb.getGithubTags("golang/go")
tags := gb.getGolangVersions()

var versions []string
for _, tag := range tags {
Expand Down Expand Up @@ -414,6 +413,9 @@ func (gb *GoBrew) judgeVersion(version string) string {
versionsSemantic = append(versionsSemantic, v)
}
}
if len(versionsSemantic) == 0 {
return ""
}

// sort semantic versions
sort.Sort(semver.Collection(versionsSemantic))
Expand All @@ -422,6 +424,9 @@ func (gb *GoBrew) judgeVersion(version string) string {
judgedVersions := groupedVersions[versionsSemantic[i].Original()]
// get last element
if version == "dev-latest" {
if len(judgedVersions) == 0 {
return ""
}
return judgedVersions[len(judgedVersions)-1]
}

Expand Down Expand Up @@ -634,41 +639,56 @@ func (gb *GoBrew) changeSymblinkGo(version string) {
}

func (gb *GoBrew) getLatestVersion() string {
tags := gb.getGithubTags("kevincobain2000/gobrew")

if len(tags) == 0 {
url := "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
data := doRequest(url)
if len(data) == 0 {
return ""
}

return tags[len(tags)-1]
type Tag struct {
TagName string `json:"tag_name"`
}
var tag Tag
utils.CheckError(json.Unmarshal(data, &tag), "==> [Error]")

return tag.TagName
}

func (gb *GoBrew) getGithubTags(repo string) (result []string) {
if len(githubTags[repo]) > 0 {
return githubTags[repo]
func (gb *GoBrew) getGolangVersions() (result []string) {
data := doRequest(goBrewTagsApi)
if len(data) == 0 {
return
}

githubTags = make(map[string][]string)
client := &http.Client{}
url := "https://api.github.com/repos/kevincobain2000/gobrew/git/refs/tags"
if repo == "golang/go" {
url = goBrewTagsApi
type Tag struct {
Ref string `json:"ref"`
}
var tags []Tag
utils.CheckError(json.Unmarshal(data, &tags), "==> [Error]")

for _, tag := range tags {
t := strings.ReplaceAll(tag.Ref, "refs/tags/", "")
if strings.HasPrefix(t, "go") {
result = append(result, t)
}
}

return
}

func doRequest(url string) (data []byte) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
color.Errorf("==> [Error] Cannot create request: %s", err)
color.Errorln("==> [Error] Cannot create request:", err.Error())
return
}

request.Header.Set("User-Agent", "gobrew")

if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}

response, err := client.Do(request)
if err != nil {
color.Errorf("==> [Error] Cannot get response: %s", err)
color.Errorln("==> [Error] Cannot get response:", err.Error())
return
}

Expand All @@ -677,34 +697,20 @@ func (gb *GoBrew) getGithubTags(repo string) (result []string) {
}(response.Body)

if response.StatusCode == http.StatusTooManyRequests {
color.Errorf("==> [Error] Rate limit exhausted")
color.Errorln("==> [Error] Rate limit exhausted")
return
}

if response.StatusCode != http.StatusOK {
color.Errorf("==> [Error] Cannot read response: %s", response.Status)
color.Errorln("==> [Error] Cannot read response:", response.Status)
return
}

data, err := io.ReadAll(response.Body)
data, err = io.ReadAll(response.Body)
if err != nil {
color.Errorf("==> [Error] Cannot read response: %s", err)
color.Errorln("==> [Error] Cannot read response Body:", err.Error())
return
}

type Tag struct {
Ref string
}
var tags []Tag
utils.CheckError(json.Unmarshal(data, &tags), "==> [Error]")

for _, tag := range tags {
t := strings.ReplaceAll(tag.Ref, "refs/tags/", "")
if strings.HasPrefix(t, "v") || strings.HasPrefix(t, "go") {
result = append(result, t)
}
}

githubTags[repo] = result
return result
return
}