Skip to content

Commit

Permalink
feat: simplifying the use of the github api
Browse files Browse the repository at this point in the history
* use prepared json to take a list of golang versions
* use a single query to take the latest version of gobrew
* now we don't need to use a token in requests
  • Loading branch information
juev committed Oct 1, 2023
1 parent 6bfcb8f commit 2353cdb
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 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 @@ -640,53 +639,44 @@ 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]
}

func (gb *GoBrew) getGithubTags(repo string) (result []string) {
if len(githubTags[repo]) > 0 {
return githubTags[repo]
type Tag struct {
TagName string `json:"tag_name"`
}
var tag Tag
utils.CheckError(json.Unmarshal(data, &tag), "==> [Error]")

githubTags = make(map[string][]string)
url := "https://api.github.com/repos/kevincobain2000/gobrew/git/refs/tags"
if repo == "golang/go" {
url = goBrewTagsApi
}
return tag.TagName
}

data := doRequest(url, os.Getenv("GITHUB_TOKEN"))
if len(data) == 0 && os.Getenv("GITHUB_TOKEN") != "" {
color.Warnln("[WARNING] invalid token we are trying a request without a token")
data = doRequest(url, "")
}
func (gb *GoBrew) getGolangVersions() (result []string) {
data := doRequest(goBrewTagsApi)
if len(data) == 0 {
return
}

type Tag struct {
Ref string
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, "v") || strings.HasPrefix(t, "go") {
if strings.HasPrefix(t, "go") {
result = append(result, t)
}
}

githubTags[repo] = result
return
}

func doRequest(url string, token string) (data []byte) {
func doRequest(url string) (data []byte) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -696,10 +686,6 @@ func doRequest(url string, token string) (data []byte) {

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

if token != "" {
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}

response, err := client.Do(request)
if err != nil {
color.Errorln("==> [Error] Cannot get response:", err.Error())
Expand Down

0 comments on commit 2353cdb

Please sign in to comment.