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