From 5b3ae2809b37d157076c973a5cbc434d7c22727b Mon Sep 17 00:00:00 2001 From: uubulb Date: Sun, 19 May 2024 22:35:24 +0800 Subject: [PATCH 1/3] Add windows10 arguments --- selfupdate/detect.go | 18 +++++++++++++----- selfupdate/update.go | 16 ++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/selfupdate/detect.go b/selfupdate/detect.go index 516b89d..cd45664 100644 --- a/selfupdate/detect.go +++ b/selfupdate/detect.go @@ -89,15 +89,23 @@ func findValidationAsset(rel *github.RepositoryRelease, validationName string) ( func findReleaseAndAsset(rels []*github.RepositoryRelease, targetVersion string, - filters []*regexp.Regexp) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { + filters []*regexp.Regexp, + isWin10 bool) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { // Generate candidates suffixes := make([]string, 0, 2*7*2) for _, sep := range []rune{'_', '-'} { for _, ext := range []string{".zip", ".tar.gz", ".tgz", ".gzip", ".gz", ".tar.xz", ".xz", ""} { suffix := fmt.Sprintf("%s%c%s%s", runtime.GOOS, sep, runtime.GOARCH, ext) + if isWin10 { + suffix := fmt.Sprintf("%s%c%s%s", "windows10", sep, runtime.GOARCH, ext) + } suffixes = append(suffixes, suffix) if runtime.GOOS == "windows" { - suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext) + if isWin10 { + suffix = fmt.Sprintf("%s%c%s.exe%s", "windows10", sep, runtime.GOARCH, ext) + } else { + suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext) + } suffixes = append(suffixes, suffix) } } @@ -136,13 +144,13 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, // 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'. -func (up *Updater) DetectLatest(slug string) (release *Release, found bool, err error) { +func (up *Updater) DetectLatest(slug string, isWin10 bool) (release *Release, found bool, err error) { return up.DetectVersion(slug, "") } // DetectVersion tries to get the given version of the repository on Github. `slug` means `owner/name` formatted string. // And version indicates the required version. -func (up *Updater) DetectVersion(slug string, version string) (release *Release, found bool, err error) { +func (up *Updater) DetectVersion(slug string, version string, isWin10 bool) (release *Release, found bool, err error) { repo := strings.Split(slug, "/") if len(repo) != 2 || repo[0] == "" || repo[1] == "" { return nil, false, fmt.Errorf("Invalid slug format. It should be 'owner/name': %s", slug) @@ -159,7 +167,7 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release, return nil, false, err } - rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters) + rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters, isWin10) if !found { return nil, false, nil } diff --git a/selfupdate/update.go b/selfupdate/update.go index 2f33c28..6107aa3 100644 --- a/selfupdate/update.go +++ b/selfupdate/update.go @@ -99,7 +99,7 @@ func (up *Updater) UpdateTo(rel *Release, cmdPath string) error { // UpdateCommand updates a given command binary to the latest version. // 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version. -func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug string) (*Release, error) { +func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug string, isWin10 bool) (*Release, error) { if runtime.GOOS == "windows" && !strings.HasSuffix(cmdPath, ".exe") { // Ensure to add '.exe' to given path on Windows cmdPath = cmdPath + ".exe" @@ -117,7 +117,7 @@ func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug st cmdPath = p } - rel, ok, err := up.DetectLatest(slug) + rel, ok, err := up.DetectLatest(slug, isWin10) if err != nil { return nil, err } @@ -138,12 +138,12 @@ func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug st // UpdateSelf updates the running executable itself to the latest version. // 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version. -func (up *Updater) UpdateSelf(current semver.Version, slug string) (*Release, error) { +func (up *Updater) UpdateSelf(current semver.Version, slug string, isWin10 bool) (*Release, error) { cmdPath, err := os.Executable() if err != nil { return nil, err } - return up.UpdateCommand(cmdPath, current, slug) + return up.UpdateCommand(cmdPath, current, slug, isWin10) } // UpdateTo downloads an executable from assetURL and replace the current binary with the downloaded one. @@ -162,12 +162,12 @@ func UpdateTo(assetURL, cmdPath string) error { // UpdateCommand updates a given command binary to the latest version. // This function is a shortcut version of updater.UpdateCommand. -func UpdateCommand(cmdPath string, current semver.Version, slug string) (*Release, error) { - return DefaultUpdater().UpdateCommand(cmdPath, current, slug) +func UpdateCommand(cmdPath string, current semver.Version, slug string, isWin10 bool) (*Release, error) { + return DefaultUpdater().UpdateCommand(cmdPath, current, slug, isWin10) } // UpdateSelf updates the running executable itself to the latest version. // This function is a shortcut version of updater.UpdateSelf. -func UpdateSelf(current semver.Version, slug string) (*Release, error) { - return DefaultUpdater().UpdateSelf(current, slug) +func UpdateSelf(current semver.Version, slug string, isWin10 bool) (*Release, error) { + return DefaultUpdater().UpdateSelf(current, slug, isWin10) } From 70b1079e49e86014786a6ec4d3e46e968070465b Mon Sep 17 00:00:00 2001 From: uubulb Date: Sun, 19 May 2024 22:53:45 +0800 Subject: [PATCH 2/3] Fix outputs --- selfupdate/detect.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/selfupdate/detect.go b/selfupdate/detect.go index cd45664..98029a3 100644 --- a/selfupdate/detect.go +++ b/selfupdate/detect.go @@ -97,7 +97,7 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, for _, ext := range []string{".zip", ".tar.gz", ".tgz", ".gzip", ".gz", ".tar.xz", ".xz", ""} { suffix := fmt.Sprintf("%s%c%s%s", runtime.GOOS, sep, runtime.GOARCH, ext) if isWin10 { - suffix := fmt.Sprintf("%s%c%s%s", "windows10", sep, runtime.GOARCH, ext) + suffix = fmt.Sprintf("%s%c%s%s", "windows10", sep, runtime.GOARCH, ext) } suffixes = append(suffixes, suffix) if runtime.GOOS == "windows" { @@ -131,7 +131,11 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, } if release == nil { - log.Println("Could not find any release for", runtime.GOOS, "and", runtime.GOARCH) + if isWin10 { + log.Println("Could not find any release for", "windows10", "and", runtime.GOARCH) + } else { + log.Println("Could not find any release for", runtime.GOOS, "and", runtime.GOARCH) + } return nil, nil, semver.Version{}, false } @@ -145,7 +149,7 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, // 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'. func (up *Updater) DetectLatest(slug string, isWin10 bool) (release *Release, found bool, err error) { - return up.DetectVersion(slug, "") + return up.DetectVersion(slug, "", isWin10) } // DetectVersion tries to get the given version of the repository on Github. `slug` means `owner/name` formatted string. @@ -204,11 +208,11 @@ func (up *Updater) DetectVersion(slug string, version string, isWin10 bool) (rel // DetectLatest detects the latest release of the slug (owner/repo). // This function is a shortcut version of updater.DetectLatest() method. -func DetectLatest(slug string) (*Release, bool, error) { - return DefaultUpdater().DetectLatest(slug) +func DetectLatest(slug string, isWin10 bool) (*Release, bool, error) { + return DefaultUpdater().DetectLatest(slug, isWin10) } // DetectVersion detects the given release of the slug (owner/repo) from its version. -func DetectVersion(slug string, version string) (*Release, bool, error) { - return DefaultUpdater().DetectVersion(slug, version) +func DetectVersion(slug string, version string, isWin10 bool) (*Release, bool, error) { + return DefaultUpdater().DetectVersion(slug, version, isWin10) } From b5a71369c12cf0d8fa2d6239c0f2aa435c61ff6b Mon Sep 17 00:00:00 2001 From: uubulb Date: Sun, 19 May 2024 23:00:39 +0800 Subject: [PATCH 3/3] More flexible --- selfupdate/detect.go | 30 +++++++++++++++--------------- selfupdate/update.go | 16 ++++++++-------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/selfupdate/detect.go b/selfupdate/detect.go index 98029a3..c2b25c3 100644 --- a/selfupdate/detect.go +++ b/selfupdate/detect.go @@ -90,19 +90,19 @@ func findValidationAsset(rel *github.RepositoryRelease, validationName string) ( func findReleaseAndAsset(rels []*github.RepositoryRelease, targetVersion string, filters []*regexp.Regexp, - isWin10 bool) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { + customOS string) (*github.RepositoryRelease, *github.ReleaseAsset, semver.Version, bool) { // Generate candidates suffixes := make([]string, 0, 2*7*2) for _, sep := range []rune{'_', '-'} { for _, ext := range []string{".zip", ".tar.gz", ".tgz", ".gzip", ".gz", ".tar.xz", ".xz", ""} { suffix := fmt.Sprintf("%s%c%s%s", runtime.GOOS, sep, runtime.GOARCH, ext) - if isWin10 { - suffix = fmt.Sprintf("%s%c%s%s", "windows10", sep, runtime.GOARCH, ext) + if len(customOS) > 0 { + suffix = fmt.Sprintf("%s%c%s%s", customOS, sep, runtime.GOARCH, ext) } suffixes = append(suffixes, suffix) if runtime.GOOS == "windows" { - if isWin10 { - suffix = fmt.Sprintf("%s%c%s.exe%s", "windows10", sep, runtime.GOARCH, ext) + if len(customOS) > 0 { + suffix = fmt.Sprintf("%s%c%s.exe%s", customOS, sep, runtime.GOARCH, ext) } else { suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext) } @@ -131,8 +131,8 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, } if release == nil { - if isWin10 { - log.Println("Could not find any release for", "windows10", "and", runtime.GOARCH) + if len(customOS) > 0 { + log.Println("Could not find any release for", customOS, "and", runtime.GOARCH) } else { log.Println("Could not find any release for", runtime.GOOS, "and", runtime.GOARCH) } @@ -148,13 +148,13 @@ func findReleaseAndAsset(rels []*github.RepositoryRelease, // 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'. -func (up *Updater) DetectLatest(slug string, isWin10 bool) (release *Release, found bool, err error) { - return up.DetectVersion(slug, "", isWin10) +func (up *Updater) DetectLatest(slug string, customOS string) (release *Release, found bool, err error) { + return up.DetectVersion(slug, "", customOS) } // DetectVersion tries to get the given version of the repository on Github. `slug` means `owner/name` formatted string. // And version indicates the required version. -func (up *Updater) DetectVersion(slug string, version string, isWin10 bool) (release *Release, found bool, err error) { +func (up *Updater) DetectVersion(slug string, version string, customOS string) (release *Release, found bool, err error) { repo := strings.Split(slug, "/") if len(repo) != 2 || repo[0] == "" || repo[1] == "" { return nil, false, fmt.Errorf("Invalid slug format. It should be 'owner/name': %s", slug) @@ -171,7 +171,7 @@ func (up *Updater) DetectVersion(slug string, version string, isWin10 bool) (rel return nil, false, err } - rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters, isWin10) + rel, asset, ver, found := findReleaseAndAsset(rels, version, up.filters, customOS) if !found { return nil, false, nil } @@ -208,11 +208,11 @@ func (up *Updater) DetectVersion(slug string, version string, isWin10 bool) (rel // DetectLatest detects the latest release of the slug (owner/repo). // This function is a shortcut version of updater.DetectLatest() method. -func DetectLatest(slug string, isWin10 bool) (*Release, bool, error) { - return DefaultUpdater().DetectLatest(slug, isWin10) +func DetectLatest(slug string, customOS string) (*Release, bool, error) { + return DefaultUpdater().DetectLatest(slug, customOS) } // DetectVersion detects the given release of the slug (owner/repo) from its version. -func DetectVersion(slug string, version string, isWin10 bool) (*Release, bool, error) { - return DefaultUpdater().DetectVersion(slug, version, isWin10) +func DetectVersion(slug string, version string, customOS string) (*Release, bool, error) { + return DefaultUpdater().DetectVersion(slug, version, customOS) } diff --git a/selfupdate/update.go b/selfupdate/update.go index 6107aa3..3b981a6 100644 --- a/selfupdate/update.go +++ b/selfupdate/update.go @@ -99,7 +99,7 @@ func (up *Updater) UpdateTo(rel *Release, cmdPath string) error { // UpdateCommand updates a given command binary to the latest version. // 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version. -func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug string, isWin10 bool) (*Release, error) { +func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug string, customOS string) (*Release, error) { if runtime.GOOS == "windows" && !strings.HasSuffix(cmdPath, ".exe") { // Ensure to add '.exe' to given path on Windows cmdPath = cmdPath + ".exe" @@ -117,7 +117,7 @@ func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug st cmdPath = p } - rel, ok, err := up.DetectLatest(slug, isWin10) + rel, ok, err := up.DetectLatest(slug, customOS) if err != nil { return nil, err } @@ -138,12 +138,12 @@ func (up *Updater) UpdateCommand(cmdPath string, current semver.Version, slug st // UpdateSelf updates the running executable itself to the latest version. // 'slug' represents 'owner/name' repository on GitHub and 'current' means the current version. -func (up *Updater) UpdateSelf(current semver.Version, slug string, isWin10 bool) (*Release, error) { +func (up *Updater) UpdateSelf(current semver.Version, slug string, customOS string) (*Release, error) { cmdPath, err := os.Executable() if err != nil { return nil, err } - return up.UpdateCommand(cmdPath, current, slug, isWin10) + return up.UpdateCommand(cmdPath, current, slug, customOS) } // UpdateTo downloads an executable from assetURL and replace the current binary with the downloaded one. @@ -162,12 +162,12 @@ func UpdateTo(assetURL, cmdPath string) error { // UpdateCommand updates a given command binary to the latest version. // This function is a shortcut version of updater.UpdateCommand. -func UpdateCommand(cmdPath string, current semver.Version, slug string, isWin10 bool) (*Release, error) { - return DefaultUpdater().UpdateCommand(cmdPath, current, slug, isWin10) +func UpdateCommand(cmdPath string, current semver.Version, slug string, customOS string) (*Release, error) { + return DefaultUpdater().UpdateCommand(cmdPath, current, slug, customOS) } // UpdateSelf updates the running executable itself to the latest version. // This function is a shortcut version of updater.UpdateSelf. -func UpdateSelf(current semver.Version, slug string, isWin10 bool) (*Release, error) { - return DefaultUpdater().UpdateSelf(current, slug, isWin10) +func UpdateSelf(current semver.Version, slug string, customOS string) (*Release, error) { + return DefaultUpdater().UpdateSelf(current, slug, customOS) }