Skip to content

Commit

Permalink
Merge pull request #95 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 6.2.1
  • Loading branch information
andyone authored Feb 18, 2017
2 parents 97b0e8f + 459f4ed commit 1e2fa0c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

#### v6.2.1

* `[usage]` Improved working with GitHub API

#### v6.2.0

* `[netutil]` Now GetIP return primary IPv4 address
Expand Down
33 changes: 28 additions & 5 deletions usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"pkg.re/essentialkaos/ek.v6/fmtc"
"pkg.re/essentialkaos/ek.v6/req"
"pkg.re/essentialkaos/ek.v6/version"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -221,7 +222,7 @@ func (about *About) Render() {
}

if about.Repository != "" {
printLatestReleaseInfo(about.Version, about.Repository)
printLatestReleaseInfo(about.App, about.Version, about.Repository)
}

fmtc.NewLine()
Expand Down Expand Up @@ -383,16 +384,16 @@ func printGroupHeader(name string) {
}

// printLatestReleaseInfo print info about latest release on GitHub
func printLatestReleaseInfo(version, repository string) {
latestRelease := getLatestRelease(repository)
func printLatestReleaseInfo(app, currentVersion, repository string) {
latestRelease := getLatestRelease(app, currentVersion, repository)

if latestRelease == nil || len(latestRelease.Tag) < 2 {
return
}

latestVersion := latestRelease.Tag[1:]

if latestVersion == version {
if !isNewerVersion(currentVersion, latestVersion) {
return
}

Expand Down Expand Up @@ -425,11 +426,12 @@ func printLatestReleaseInfo(version, repository string) {
}

// getLatestRelease fetch latest release from GitHub
func getLatestRelease(repository string) *release {
func getLatestRelease(app, version, repository string) *release {
engine := req.Engine{}

engine.SetDialTimeout(2)
engine.SetRequestTimeout(2)
engine.SetUserAgent(app, version, "go.ek/6")

response, err := engine.Get(req.Request{
URL: "https://api.github.com/repos/" + repository + "/releases/latest",
Expand All @@ -440,6 +442,10 @@ func getLatestRelease(repository string) *release {
return nil
}

if response.Header.Get("X-RateLimit-Remaining") == "0" {
return nil
}

var rel = &release{}

err = response.JSON(rel)
Expand All @@ -450,3 +456,20 @@ func getLatestRelease(repository string) *release {

return rel
}

// isNewerVersion return true if latest version is greater than current
func isNewerVersion(current, latest string) bool {
v1, err := version.Parse(current)

if err != nil {
return false
}

v2, err := version.Parse(latest)

if err != nil {
return false
}

return v2.Greater(v1)
}

0 comments on commit 1e2fa0c

Please sign in to comment.