1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00

fix updater

This commit is contained in:
Jesse Duffield 2020-11-21 18:58:55 +11:00
parent d02e992265
commit f7e6d4e724
2 changed files with 48 additions and 14 deletions

View File

@ -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

View File

@ -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
}
func (u *Updater) zipExtension() string {
if runtime.GOOS == "windows" {
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) {
extension := "tar.gz"
if runtime.GOOS == "windows" {
extension = "zip"
}
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
}