2022-12-30 14:24:24 +02:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/updates"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpdateHelper struct {
|
2023-03-23 03:35:07 +02:00
|
|
|
c *HelperCommon
|
2022-12-30 14:24:24 +02:00
|
|
|
updater *updates.Updater
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:35:07 +02:00
|
|
|
func NewUpdateHelper(c *HelperCommon, updater *updates.Updater) *UpdateHelper {
|
2022-12-30 14:24:24 +02:00
|
|
|
return &UpdateHelper{
|
|
|
|
c: c,
|
|
|
|
updater: updater,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 06:32:41 +02:00
|
|
|
func (self *UpdateHelper) CheckForUpdateInBackground() {
|
2022-12-30 14:24:24 +02:00
|
|
|
self.updater.CheckForNewUpdate(func(newVersion string, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
// ignoring the error for now so that I'm not annoying users
|
|
|
|
self.c.Log.Error(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newVersion == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if self.c.UserConfig.Update.Method == "background" {
|
|
|
|
self.startUpdating(newVersion)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return self.showUpdatePrompt(newVersion)
|
|
|
|
}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *UpdateHelper) CheckForUpdateInForeground() error {
|
|
|
|
return self.c.WithWaitingStatus(self.c.Tr.CheckingForUpdates, func() error {
|
|
|
|
self.updater.CheckForNewUpdate(func(newVersion string, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
if newVersion == "" {
|
|
|
|
return self.c.ErrorMsg(self.c.Tr.FailedToRetrieveLatestVersionErr)
|
|
|
|
}
|
|
|
|
return self.showUpdatePrompt(newVersion)
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *UpdateHelper) startUpdating(newVersion string) {
|
|
|
|
_ = self.c.WithWaitingStatus(self.c.Tr.UpdateInProgressWaitingStatus, func() error {
|
|
|
|
self.c.State().SetUpdating(true)
|
|
|
|
err := self.updater.Update(newVersion)
|
|
|
|
return self.onUpdateFinish(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *UpdateHelper) onUpdateFinish(err error) error {
|
|
|
|
self.c.State().SetUpdating(false)
|
|
|
|
self.c.OnUIThread(func() error {
|
|
|
|
self.c.SetViewContent(self.c.Views().AppStatus, "")
|
|
|
|
if err != nil {
|
|
|
|
errMessage := utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.UpdateFailedErr, map[string]string{
|
|
|
|
"errMessage": err.Error(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return self.c.ErrorMsg(errMessage)
|
|
|
|
}
|
|
|
|
return self.c.Alert(self.c.Tr.UpdateCompletedTitle, self.c.Tr.UpdateCompleted)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *UpdateHelper) showUpdatePrompt(newVersion string) error {
|
|
|
|
message := utils.ResolvePlaceholderString(
|
|
|
|
self.c.Tr.UpdateAvailable, map[string]string{
|
|
|
|
"newVersion": newVersion,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
|
|
|
Title: self.c.Tr.UpdateAvailableTitle,
|
|
|
|
Prompt: message,
|
|
|
|
HandleConfirm: func() error {
|
|
|
|
self.startUpdating(newVersion)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|