Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for custom (GOOS and GOARCH) suffix #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions selfupdate/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 23 additions & 2 deletions selfupdate/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Updater struct {
apiCtx context.Context
validator Validator
filters []*regexp.Regexp
suffix string
}

// Config represents the configuration of self-update.
Expand All @@ -37,6 +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
// 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 {
Expand Down Expand Up @@ -71,7 +92,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
Expand All @@ -82,7 +103,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.
Expand Down