From e059209c995c454029d342b06c02f5d81956f164 Mon Sep 17 00:00:00 2001 From: svanas Date: Wed, 13 Apr 2022 12:32:03 +0200 Subject: [PATCH 1/2] allow for custom (GOOS and GOARCH) suffix --- selfupdate/detect.go | 27 +++++++++++++++++---------- selfupdate/updater.go | 6 ++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/selfupdate/detect.go b/selfupdate/detect.go index 6ca1af7..ae896bc 100644 --- a/selfupdate/detect.go +++ b/selfupdate/detect.go @@ -89,19 +89,26 @@ 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, + suffix 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) - suffixes = append(suffixes, suffix) - if runtime.GOOS == "windows" { - suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext) + suffixes := func() []string { + if suffix != "" { + return []string{suffix} + } + 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) suffixes = append(suffixes, suffix) + if runtime.GOOS == "windows" { + suffix = fmt.Sprintf("%s%c%s.exe%s", runtime.GOOS, sep, runtime.GOARCH, ext) + suffixes = append(suffixes, suffix) + } } } - } + return suffixes + }() var ver semver.Version var asset *github.ReleaseAsset @@ -159,7 +166,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, up.suffix) if !found { return nil, false, nil } diff --git a/selfupdate/updater.go b/selfupdate/updater.go index 32cf5e0..7824e80 100644 --- a/selfupdate/updater.go +++ b/selfupdate/updater.go @@ -19,6 +19,7 @@ type Updater struct { apiCtx context.Context validator Validator filters []*regexp.Regexp + suffix string } // Config represents the configuration of self-update. @@ -37,6 +38,7 @@ type Config struct { // An asset is selected if it matches any of those, in addition to the regular tag, os, arch, extensions. // Please make sure that your filter(s) uniquely match an asset. Filters []string + Suffix string } func newHTTPClient(ctx context.Context, token string) *http.Client { @@ -71,7 +73,7 @@ func NewUpdater(config Config) (*Updater, error) { if config.EnterpriseBaseURL == "" { client := github.NewClient(hc) - return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe}, nil + return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe, suffix: config.Suffix}, nil } u := config.EnterpriseUploadURL @@ -82,7 +84,7 @@ func NewUpdater(config Config) (*Updater, error) { if err != nil { return nil, err } - return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe}, nil + return &Updater{api: client, apiCtx: ctx, validator: config.Validator, filters: filtersRe, suffix: config.Suffix}, nil } // DefaultUpdater creates a new updater instance with default configuration. From 5e8257128a6b81d0089732934800135592da8412 Mon Sep 17 00:00:00 2001 From: svanas Date: Thu, 14 Apr 2022 08:48:43 +0200 Subject: [PATCH 2/2] added Config.Suffix docs --- selfupdate/updater.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/selfupdate/updater.go b/selfupdate/updater.go index 7824e80..2fc6bcc 100644 --- a/selfupdate/updater.go +++ b/selfupdate/updater.go @@ -38,7 +38,26 @@ type Config struct { // An asset is selected if it matches any of those, in addition to the regular tag, os, arch, extensions. // Please make sure that your filter(s) uniquely match an asset. Filters []string - Suffix string + // self-update assumes that released binaries are in this format: {cmd}_{goos}_{goarch}{.ext}, where + // {cmd} is the name of command, and + // {goos} and {arch} are the platform and the architecture, and + // {.ext} is the file extension. + // should you want to deviate from the above format, you can do so with Suffix, for example: + // updater, err := selfupdate.NewUpdater(selfupdate.Config{ + // Suffix: func() string { + // if runtime.GOOS == "windows" { + // return fmt.Sprintf("Win%s.exe", func() string { + // if runtime.GOARCH == "386" { + // return "32" + // } else { + // return "64" + // } + // }()) + // } + // return fmt.Sprintf("macOS-%s", runtime.GOARCH) + // }(), + // }) + Suffix string } func newHTTPClient(ctx context.Context, token string) *http.Client {