go-github-selfupdate/selfupdate/update.go

123 lines
4.2 KiB
Go
Raw Normal View History

package selfupdate
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/blang/semver"
"github.com/inconshreveable/go-update"
)
func readAndUpdate(src io.ReadCloser, assetURL, cmdPath string) error {
defer src.Close()
_, cmd := filepath.Split(cmdPath)
asset, err := UncompressCommand(src, assetURL, cmd)
if err != nil {
return err
}
log.Println("Will update", cmdPath, "to the latest downloaded from", assetURL)
return update.Apply(asset, update.Options{
TargetPath: cmdPath,
})
}
// UpdateTo downloads an executable from assetURL and replace the current binary with the downloaded one.
// This function is low-level API to update the binary. Because it does not use GitHub API and downloads asset directly from the URL via HTTP,
// this function is not available to update a release for private repositories.
// cmdPath is a file path to command executable.
func UpdateTo(assetURL, cmdPath string) error {
res, err := http.Get(assetURL)
if err != nil {
return fmt.Errorf("Failed to download a release file from %s: %s", assetURL, err)
}
if res.StatusCode != 200 {
return fmt.Errorf("Failed to download a release file from %s", assetURL)
}
return readAndUpdate(res.Body, assetURL, cmdPath)
}
// UpdateTo downloads an executable from GitHub Releases API and replace current binary with the downloaded one.
// It downloads a release asset via GitHub Releases API so this function is available for update releases on private repository.
// If a redirect occurs, it fallbacks into directly downloading from the redirect URL.
func (up *Updater) UpdateTo(rel *Release, cmdPath string) error {
src, redirectURL, err := up.api.Repositories.DownloadReleaseAsset(up.apiCtx, rel.RepoOwner, rel.RepoName, rel.AssetID)
if err != nil {
return err
}
if redirectURL != "" {
log.Println("Redirect URL was returned while trying to download a release asset from GitHub API. Falling back to downloading from asset URL directly:", redirectURL)
return UpdateTo(redirectURL, cmdPath)
}
return readAndUpdate(src, rel.AssetURL, cmdPath)
}
// 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) {
if runtime.GOOS == "windows" && !strings.HasSuffix(cmdPath, ".exe") {
2018-01-01 07:01:42 +00:00
// Ensure to add '.exe' to given path on Windows
cmdPath = cmdPath + ".exe"
}
stat, err := os.Lstat(cmdPath)
if err != nil {
return nil, fmt.Errorf("Failed to stat '%s'. File may not exist: %s", cmdPath, err)
}
if stat.Mode()&os.ModeSymlink != 0 {
p, err := filepath.EvalSymlinks(cmdPath)
if err != nil {
return nil, fmt.Errorf("Failed to resolve symlink '%s' for executable: %s", cmdPath, err)
}
cmdPath = p
}
rel, ok, err := up.DetectLatest(slug)
if err != nil {
return nil, err
}
if !ok {
2017-12-30 04:25:42 +00:00
log.Println("No release detected. Current version is considered up-to-date")
return &Release{Version: current}, nil
}
if current.Equals(rel.Version) {
2017-12-30 04:25:42 +00:00
log.Println("Current version", current, "is the latest. Update is not needed")
return rel, nil
}
2017-12-30 04:25:42 +00:00
log.Println("Will update", cmdPath, "to the latest version", rel.Version)
if err := up.UpdateTo(rel, cmdPath); err != nil {
return nil, err
}
return rel, nil
}
// 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) {
cmdPath, err := os.Executable()
if err != nil {
return nil, err
}
return UpdateCommand(cmdPath, current, slug)
}
// 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 NewUpdater(Config{}).UpdateCommand(cmdPath, current, slug)
}
// 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 NewUpdater(Config{}).UpdateSelf(current, slug)
}