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

Add windows10 arguments #2

Closed
wants to merge 3 commits into from
Closed
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
34 changes: 23 additions & 11 deletions selfupdate/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 len(customOS) > 0 {
suffix = fmt.Sprintf("%s%c%s%s", customOS, 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 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)
}
suffixes = append(suffixes, suffix)
}
}
Expand All @@ -123,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 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)
}
return nil, nil, semver.Version{}, false
}

Expand All @@ -136,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) (release *Release, found bool, err error) {
return up.DetectVersion(slug, "")
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) (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)
Expand All @@ -159,7 +171,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, customOS)
if !found {
return nil, false, nil
}
Expand Down Expand Up @@ -196,11 +208,11 @@ func (up *Updater) DetectVersion(slug string, version string) (release *Release,

// 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, 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) (*Release, bool, error) {
return DefaultUpdater().DetectVersion(slug, version)
func DetectVersion(slug string, version string, customOS string) (*Release, bool, error) {
return DefaultUpdater().DetectVersion(slug, version, customOS)
}
16 changes: 8 additions & 8 deletions selfupdate/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, customOS string) (*Release, error) {
if runtime.GOOS == "windows" && !strings.HasSuffix(cmdPath, ".exe") {
// Ensure to add '.exe' to given path on Windows
cmdPath = cmdPath + ".exe"
Expand All @@ -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, customOS)
if err != nil {
return nil, err
}
Expand All @@ -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, customOS string) (*Release, error) {
cmdPath, err := os.Executable()
if err != nil {
return nil, err
}
return up.UpdateCommand(cmdPath, current, slug)
return up.UpdateCommand(cmdPath, current, slug, customOS)
}

// UpdateTo downloads an executable from assetURL and replace the current binary with the downloaded one.
Expand All @@ -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, 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) (*Release, error) {
return DefaultUpdater().UpdateSelf(current, slug)
func UpdateSelf(current semver.Version, slug string, customOS string) (*Release, error) {
return DefaultUpdater().UpdateSelf(current, slug, customOS)
}