2017-12-26 02:45:08 +00:00
|
|
|
package selfupdate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-12-26 03:02:00 +00:00
|
|
|
"regexp"
|
2017-12-26 09:45:52 +00:00
|
|
|
"runtime"
|
2017-12-26 02:45:08 +00:00
|
|
|
"strings"
|
2018-01-18 10:33:31 +00:00
|
|
|
|
2021-07-24 21:09:12 +00:00
|
|
|
"github.com/blang/semver/v4"
|
2020-04-10 00:28:13 +00:00
|
|
|
"github.com/google/go-github/v30/github"
|
2017-12-26 02:45:08 +00:00
|
|
|
)
|
|
|
|
|
2017-12-26 03:02:00 +00:00
|
|
|
var reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
|
|
|
|
|
2019-12-16 19:12:11 +00:00
|
|
|
func findAssetFromRelease(rel *github.RepositoryRelease,
|
2019-12-18 08:32:48 +00:00
|
|
|
suffixes []string, targetVersion string, filters []*regexp.Regexp) (*github.ReleaseAsset, semver.Version, bool) {
|
2019-12-16 19:12:11 +00:00
|
|
|
|
2018-02-02 11:24:01 +00:00
|
|
|
if targetVersion != "" && targetVersion != rel.GetTagName() {
|
|
|
|
log.Println("Skip", rel.GetTagName(), "not matching to specified version", targetVersion)
|
|
|
|
return nil, semver.Version{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetVersion == "" && rel.GetDraft() {
|
2018-02-02 09:02:27 +00:00
|
|
|
log.Println("Skip draft version", rel.GetTagName())
|
2018-02-11 13:57:15 +00:00
|
|
|
return nil, semver.Version{}, false
|
2018-02-02 09:02:27 +00:00
|
|
|
}
|
2018-02-02 11:24:01 +00:00
|
|
|
if targetVersion == "" && rel.GetPrerelease() {
|
2018-02-02 09:02:27 +00:00
|
|
|
log.Println("Skip pre-release version", rel.GetTagName())
|
2018-02-11 13:57:15 +00:00
|
|
|
return nil, semver.Version{}, false
|
2018-02-02 09:02:27 +00:00
|
|
|
}
|
2018-02-11 13:57:15 +00:00
|
|
|
|
|
|
|
verText := rel.GetTagName()
|
|
|
|
indices := reVersion.FindStringIndex(verText)
|
|
|
|
if indices == nil {
|
2018-02-11 14:50:21 +00:00
|
|
|
log.Println("Skip version not adopting semver", verText)
|
2018-02-11 13:57:15 +00:00
|
|
|
return nil, semver.Version{}, false
|
|
|
|
}
|
|
|
|
if indices[0] > 0 {
|
|
|
|
log.Println("Strip prefix of version", verText[:indices[0]], "from", verText)
|
|
|
|
verText = verText[indices[0]:]
|
2018-02-02 09:02:27 +00:00
|
|
|
}
|
2018-02-11 13:57:15 +00:00
|
|
|
|
2018-02-11 15:05:51 +00:00
|
|
|
// If semver cannot parse the version text, it means that the text is not adopting
|
|
|
|
// the semantic versioning. So it should be skipped.
|
|
|
|
ver, err := semver.Make(verText)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to parse a semantic version", verText)
|
|
|
|
return nil, semver.Version{}, false
|
|
|
|
}
|
|
|
|
|
2018-02-02 09:02:27 +00:00
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
name := asset.GetName()
|
2019-12-18 08:32:48 +00:00
|
|
|
if len(filters) > 0 {
|
|
|
|
// if some filters are defined, match them: if any one matches, the asset is selected
|
|
|
|
matched := false
|
|
|
|
for _, filter := range filters {
|
|
|
|
if filter.MatchString(name) {
|
|
|
|
log.Println("Selected filtered asset", name)
|
|
|
|
matched = true
|
2019-12-16 19:12:11 +00:00
|
|
|
break
|
|
|
|
}
|
2019-12-18 17:34:02 +00:00
|
|
|
log.Printf("Skipping asset %q not matching filter %v\n", name, filter)
|
2019-12-18 08:32:48 +00:00
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range suffixes {
|
|
|
|
if strings.HasSuffix(name, s) { // require version, arch etc
|
2019-12-16 19:12:11 +00:00
|
|
|
// default: assume single artifact
|
2020-04-10 00:28:13 +00:00
|
|
|
return asset, ver, true
|
2018-02-02 09:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-11 13:57:15 +00:00
|
|
|
|
2018-02-06 04:47:59 +00:00
|
|
|
log.Println("No suitable asset was found in release", rel.GetTagName())
|
2018-02-11 13:57:15 +00:00
|
|
|
return nil, semver.Version{}, false
|
2018-02-02 09:02:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 15:28:06 +00:00
|
|
|
func findValidationAsset(rel *github.RepositoryRelease, validationName string) (*github.ReleaseAsset, bool) {
|
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
if asset.GetName() == validationName {
|
2020-04-10 00:28:13 +00:00
|
|
|
return asset, true
|
2018-11-06 15:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2019-12-16 19:12:11 +00:00
|
|
|
func findReleaseAndAsset(rels []*github.RepositoryRelease,
|
2019-12-18 08:32:48 +00:00
|
|
|
targetVersion string,
|
|
|
|
filters []*regexp.Regexp) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) {
|
2018-02-02 11:24:01 +00:00
|
|
|
// Generate candidates
|
2018-02-02 09:02:27 +00:00
|
|
|
suffixes := make([]string, 0, 2*7*2)
|
2017-12-26 09:45:52 +00:00
|
|
|
for _, sep := range []rune{'_', '-'} {
|
2019-10-19 10:46:25 +00:00
|
|
|
for _, ext := range []string{".zip", ".tar.gz", ".tgz", ".gzip", ".gz", ".tar.xz", ".xz", ""} {
|
2017-12-27 04:08:16 +00:00
|
|
|
suffix := fmt.Sprintf("%s%c%s%s", runtime.GOOS, sep, runtime.GOARCH, ext)
|
2018-02-02 09:02:27 +00:00
|
|
|
suffixes = append(suffixes, suffix)
|
2017-12-26 09:45:52 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2017-12-27 04:08:16 +00:00
|
|
|
suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext)
|
2018-02-02 09:02:27 +00:00
|
|
|
suffixes = append(suffixes, suffix)
|
2017-12-26 09:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:22:08 +00:00
|
|
|
var ver semver.Version
|
|
|
|
var asset *github.ReleaseAsset
|
|
|
|
var release *github.RepositoryRelease
|
2018-02-11 13:57:15 +00:00
|
|
|
|
2018-02-11 14:22:08 +00:00
|
|
|
// Find the latest version from the list of releases.
|
|
|
|
// Returned list from GitHub API is in the order of the date when created.
|
|
|
|
// ref: https://github.com/rhysd/go-github-selfupdate/issues/11
|
2017-12-26 09:45:52 +00:00
|
|
|
for _, rel := range rels {
|
2019-12-18 08:32:48 +00:00
|
|
|
if a, v, ok := findAssetFromRelease(rel, suffixes, targetVersion, filters); ok {
|
2018-02-11 14:22:08 +00:00
|
|
|
// Note: any version with suffix is less than any version without suffix.
|
|
|
|
// e.g. 0.0.1 > 0.0.1-beta
|
|
|
|
if release == nil || v.GTE(ver) {
|
|
|
|
ver = v
|
|
|
|
asset = a
|
|
|
|
release = rel
|
|
|
|
}
|
2017-12-26 09:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:22:08 +00:00
|
|
|
if release == nil {
|
|
|
|
log.Println("Could not find any release for", runtime.GOOS, "and", runtime.GOARCH)
|
|
|
|
return nil, nil, semver.Version{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return release, asset, ver, true
|
2017-12-26 09:45:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 02:45:08 +00:00
|
|
|
// DetectLatest tries to get the latest version of the repository on GitHub. 'slug' means 'owner/name' formatted string.
|
2018-02-02 09:02:27 +00:00
|
|
|
// It fetches releases information from GitHub API and find out the latest release with matching the tag names and asset names.
|
2018-03-21 10:18:46 +00:00
|
|
|
// Drafts and pre-releases are ignored. Assets would be suffixed by the OS name and the arch name such as 'foo_linux_amd64'
|
2018-02-02 09:02:27 +00:00
|
|
|
// where 'foo' is a command name. '-' can also be used as a separator. File can be compressed with zip, gzip, zxip, tar&zip or tar&zxip.
|
|
|
|
// So the asset can have a file extension for the corresponding compression format such as '.zip'.
|
|
|
|
// On Windows, '.exe' also can be contained such as 'foo_windows_amd64.exe.zip'.
|
2019-12-18 08:32:48 +00:00
|
|
|
func (up *Updater) DetectLatest(slug string) (release *Release, found bool, err error) {
|
|
|
|
return up.DetectVersion(slug, "")
|
2018-02-02 11:24:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 10:18:46 +00:00
|
|
|
// DetectVersion tries to get the given version of the repository on Github. `slug` means `owner/name` formatted string.
|
2018-02-02 11:24:01 +00:00
|
|
|
// And version indicates the required version.
|
2019-12-18 08:32:48 +00:00
|
|
|
func (up *Updater) DetectVersion(slug string, version string) (release *Release, found bool, err error) {
|
2017-12-26 02:45:08 +00:00
|
|
|
repo := strings.Split(slug, "/")
|
|
|
|
if len(repo) != 2 || repo[0] == "" || repo[1] == "" {
|
2018-02-11 14:50:21 +00:00
|
|
|
return nil, false, fmt.Errorf("Invalid slug format. It should be 'owner/name': %s", slug)
|
2017-12-26 02:45:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 02:28:59 +00:00
|
|
|
rels, res, err := up.api.Repositories.ListReleases(up.apiCtx, repo[0], repo[1], nil)
|
2017-12-26 02:45:08 +00:00
|
|
|
if err != nil {
|
2017-12-26 03:02:00 +00:00
|
|
|
log.Println("API returned an error response:", err)
|
2017-12-29 09:14:43 +00:00
|
|
|
if res != nil && res.StatusCode == 404 {
|
2017-12-26 02:45:08 +00:00
|
|
|
// 404 means repository not found or release not found. It's not an error here.
|
|
|
|
err = nil
|
|
|
|
log.Println("API returned 404. Repository or release not found")
|
|
|
|
}
|
2018-02-11 14:50:21 +00:00
|
|
|
return nil, false, err
|
2017-12-26 02:45:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 08:32:48 +00:00
|
|
|
rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters)
|
2017-12-26 09:45:52 +00:00
|
|
|
if !found {
|
2018-02-11 14:50:21 +00:00
|
|
|
return nil, false, nil
|
2017-12-26 09:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
url := asset.GetBrowserDownloadURL()
|
2018-02-11 13:57:15 +00:00
|
|
|
log.Println("Successfully fetched the latest release. tag:", rel.GetTagName(), ", name:", rel.GetName(), ", URL:", rel.GetURL(), ", Asset:", url)
|
2017-12-26 03:02:00 +00:00
|
|
|
|
2017-12-31 08:30:54 +00:00
|
|
|
publishedAt := rel.GetPublishedAt().Time
|
2018-02-02 11:24:01 +00:00
|
|
|
release = &Release{
|
2018-02-11 14:50:21 +00:00
|
|
|
ver,
|
|
|
|
url,
|
|
|
|
asset.GetSize(),
|
|
|
|
asset.GetID(),
|
2018-11-06 15:28:06 +00:00
|
|
|
-1,
|
2018-02-11 14:50:21 +00:00
|
|
|
rel.GetHTMLURL(),
|
|
|
|
rel.GetBody(),
|
|
|
|
rel.GetName(),
|
|
|
|
&publishedAt,
|
|
|
|
repo[0],
|
|
|
|
repo[1],
|
2017-12-27 03:16:26 +00:00
|
|
|
}
|
2018-11-06 15:28:06 +00:00
|
|
|
|
|
|
|
if up.validator != nil {
|
2019-12-16 19:12:11 +00:00
|
|
|
validationName := asset.GetName() + up.validator.Suffix()
|
2018-11-06 15:28:06 +00:00
|
|
|
validationAsset, ok := findValidationAsset(rel, validationName)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("Failed finding validation file %q", validationName)
|
|
|
|
}
|
|
|
|
release.ValidationAssetID = validationAsset.GetID()
|
|
|
|
}
|
|
|
|
|
2018-02-11 14:50:21 +00:00
|
|
|
return release, true, nil
|
2017-12-26 02:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 03:02:00 +00:00
|
|
|
// DetectLatest detects the latest release of the slug (owner/repo).
|
2018-02-02 09:02:27 +00:00
|
|
|
// This function is a shortcut version of updater.DetectLatest() method.
|
2019-12-18 08:32:48 +00:00
|
|
|
func DetectLatest(slug string) (*Release, bool, error) {
|
|
|
|
return DefaultUpdater().DetectLatest(slug)
|
2017-12-26 02:45:08 +00:00
|
|
|
}
|
2018-02-02 11:24:01 +00:00
|
|
|
|
|
|
|
// DetectVersion detects the given release of the slug (owner/repo) from its version.
|
2019-12-18 08:32:48 +00:00
|
|
|
func DetectVersion(slug string, version string) (*Release, bool, error) {
|
|
|
|
return DefaultUpdater().DetectVersion(slug, version)
|
2018-02-02 11:24:01 +00:00
|
|
|
}
|