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

Improve code that reads the latest release from GitHub #323

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Changes from 1 commit
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
14 changes: 5 additions & 9 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/blang/semver/v4"
)

type github_releases []struct {
type github_release struct {
Assets []interface{} `json:"assets"`
AssetsURL string `json:"assets_url"`
Author struct {
Expand Down Expand Up @@ -92,7 +92,6 @@ func updateCheck(ctx *ntcontext) {
if currentVersion.Compare(latestVersion) == -1 {
ctx.update.available = true
}

}

func update(ctx *ntcontext) {
Expand Down Expand Up @@ -147,7 +146,6 @@ func fetchFile(file string) ([]byte, error) {
return nil, err
}
return body, nil

}

func publickey() []byte {
Expand All @@ -160,7 +158,7 @@ func publickey() []byte {
}

func getLatestRelease() (string, error) {
url := "https://api.github.com/repos/noisetorch/NoiseTorch/releases?per_page=1&page=1"
url := "https://api.github.com/repos/noisetorch/NoiseTorch/releases/latest"

httpclient := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
Expand Down Expand Up @@ -190,16 +188,14 @@ func getLatestRelease() (string, error) {
log.Fatal(readErr)
}

respBytes := []byte(body)

var latest_release github_releases
var latest_release github_release

err = json.Unmarshal(respBytes, &latest_release)
err = json.Unmarshal(body, &latest_release)
if err != nil {
log.Println("Reading JSON for latest_release failed", err)
// Return an empty string when the JSON is something unexpected, for example: when rate limited
return "", err
}

return latest_release[0].TagName, nil
return latest_release.TagName, nil
}