diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-11-21 18:58:55 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-11-22 10:00:35 +1100 |
commit | f7e6d4e7248293dc1a70a2c616d8a5334dee07f8 (patch) | |
tree | 5a95fcc50422cadf0888d7ab88a47d96b353cc4c | |
parent | d02e99226542ae37f012867ff04cc19167e478f8 (diff) |
fix updater
-rw-r--r-- | pkg/gui/updates.go | 8 | ||||
-rw-r--r-- | pkg/updates/updates.go | 54 |
2 files changed, 48 insertions, 14 deletions
diff --git a/pkg/gui/updates.go b/pkg/gui/updates.go index 02557deb9..5f550dbd6 100644 --- a/pkg/gui/updates.go +++ b/pkg/gui/updates.go @@ -1,11 +1,15 @@ package gui -import "github.com/jesseduffield/gocui" +import ( + "fmt" + + "github.com/jesseduffield/gocui" +) func (gui *Gui) showUpdatePrompt(newVersion string) error { return gui.ask(askOpts{ title: "New version available!", - prompt: "Download latest version? (enter/esc)", + prompt: fmt.Sprintf("Download version %s? (enter/esc)", newVersion), handleConfirm: func() error { gui.startUpdating(newVersion) return nil diff --git a/pkg/updates/updates.go b/pkg/updates/updates.go index ba7df37ac..6b570d837 100644 --- a/pkg/updates/updates.go +++ b/pkg/updates/updates.go @@ -92,9 +92,18 @@ func (u *Updater) majorVersionDiffers(oldVersion, newVersion string) bool { return strings.Split(oldVersion, ".")[0] != strings.Split(newVersion, ".")[0] } +func (u *Updater) currentVersion() string { + versionNumber := u.Config.GetVersion() + if versionNumber == "unversioned" { + return versionNumber + } + + return fmt.Sprintf("v%s", u.Config.GetVersion()) +} + func (u *Updater) checkForNewUpdate() (string, error) { u.Log.Info("Checking for an updated version") - currentVersion := u.Config.GetVersion() + currentVersion := u.currentVersion() if err := u.RecordLastUpdateCheck(); err != nil { return "", err } @@ -214,12 +223,16 @@ func (u *Updater) mappedArch(arch string) string { return arch } -// example: https://github.com/jesseduffield/lazygit/releases/download/v0.1.73/lazygit_0.1.73_Darwin_x86_64.tar.gz -func (u *Updater) getBinaryUrl(newVersion string) (string, error) { - extension := "tar.gz" +func (u *Updater) zipExtension() string { if runtime.GOOS == "windows" { - extension = "zip" + return "zip" } + + return "tar.gz" +} + +// example: https://github.com/jesseduffield/lazygit/releases/download/v0.1.73/lazygit_0.1.73_Darwin_x86_64.tar.gz +func (u *Updater) getBinaryUrl(newVersion string) (string, error) { url := fmt.Sprintf( "%s/releases/download/%s/lazygit_%s_%s_%s.%s", PROJECT_URL, @@ -227,7 +240,7 @@ func (u *Updater) getBinaryUrl(newVersion string) (string, error) { newVersion[1:], u.mappedOs(runtime.GOOS), u.mappedArch(runtime.GOARCH), - extension, + u.zipExtension(), ) u.Log.Info("Url for latest release is " + url) return url, nil @@ -256,11 +269,16 @@ func (u *Updater) downloadAndInstall(rawUrl string) error { configDir := u.Config.GetUserConfigDir() u.Log.Info("Download directory is " + configDir) - tempPath := filepath.Join(configDir, "temp_lazygit") - u.Log.Info("Temp path to binary is " + tempPath) + zipPath := filepath.Join(configDir, "temp_lazygit."+u.zipExtension()) + u.Log.Info("Temp path to tarball/zip file is " + zipPath) - // Create the file - out, err := os.Create(tempPath) + // remove existing zip file + if err := os.RemoveAll(zipPath); err != nil && !os.IsNotExist(err) { + return err + } + + // Create the zip file + out, err := os.Create(zipPath) if err != nil { return err } @@ -284,6 +302,18 @@ func (u *Updater) downloadAndInstall(rawUrl string) error { return err } + u.Log.Info("untarring tarball/unzipping zip file") + if err := u.OSCommand.RunCommand("tar -zxf %s %s", u.OSCommand.Quote(zipPath), "lazygit"); err != nil { + return err + } + + // the `tar` terminal cannot store things in a new location without permission + // so it creates it in the current directory. As such our path is fairly simple. + // You won't see it because it's gitignored. + tempLazygitFilePath := "lazygit" + + u.Log.Infof("Path to temp binary is %s", tempLazygitFilePath) + // get the path of the current binary binaryPath, err := osext.Executable() if err != nil { @@ -292,12 +322,12 @@ func (u *Updater) downloadAndInstall(rawUrl string) error { u.Log.Info("Binary path is " + binaryPath) // Verify the main file exists - if _, err := os.Stat(tempPath); err != nil { + if _, err := os.Stat(zipPath); err != nil { return err } // swap out the old binary for the new one - err = os.Rename(tempPath, binaryPath) + err = os.Rename(tempLazygitFilePath, binaryPath) if err != nil { return err } |