forked from yext/edward
/
updates.go
47 lines (38 loc) · 1.34 KB
/
updates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package updates
import (
"context"
"log"
"strings"
"github.com/google/go-github/github"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
)
// UpdateAvailable determines if a newer version is available given a repo
func UpdateAvailable(owner, repo, currentVersion, cachePath string) (bool, string, error) {
diskCache := diskcache.New(cachePath)
transport := httpcache.NewTransport(diskCache)
client := github.NewClient(transport.Client())
release, _, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo)
if err != nil {
// Log, but don't return rate limit errors
if _, ok := err.(*github.RateLimitError); ok {
log.Printf("Rate limit error when requesting latest version %v", err)
return false, "", nil
}
return false, "", errors.WithStack(err)
}
latestVersion := *release.TagName
latestVersion = strings.Replace(latestVersion, "v", "", 1)
log.Printf("Comparing latest release %v, to current version %v\n", latestVersion, currentVersion)
lv, err1 := version.NewVersion(latestVersion)
cv, err2 := version.NewVersion(currentVersion)
if err1 != nil {
return false, latestVersion, errors.WithStack(err)
}
if err2 != nil {
return true, latestVersion, errors.WithStack(err)
}
return cv.LessThan(lv), latestVersion, nil
}