diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-23 18:43:16 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-23 18:43:16 +1000 |
commit | 5628eae502ee3b4d3d72a8e4045713b637f7a764 (patch) | |
tree | 70b2af957b70667b0d80571164df9d6e65f3777c | |
parent | 954dfb12e466d1fe47231adec977a595730f5836 (diff) |
WIP
-rw-r--r-- | pkg/gui/confirmation_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 8 | ||||
-rw-r--r-- | pkg/gui/gui.go | 103 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 9 | ||||
-rw-r--r-- | pkg/updates/updates.go | 117 |
5 files changed, 178 insertions, 61 deletions
diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 025753d56..9d91b53bc 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -18,7 +18,7 @@ func (gui *Gui) wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.Vie return func(g *gocui.Gui, v *gocui.View) error { if function != nil { if err := function(g, v); err != nil { - panic(err) + return err } } return gui.closeConfirmationPrompt(g) diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 5791a9d15..7ddb50811 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -251,6 +251,14 @@ func (gui *Gui) handleFileEdit(g *gocui.Gui, v *gocui.View) error { return gui.genericFileOpen(g, v, file.Name, gui.OSCommand.EditFile) } +func (gui *Gui) openFile(filename string) error { + err := gui.OSCommand.OpenFile(filename) + if err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + return nil +} + func (gui *Gui) handleFileOpen(g *gocui.Gui, v *gocui.View) error { file, err := gui.getSelectedFile(g) if err != nil { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index d1b41e4d0..baed39056 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -80,6 +80,7 @@ type guiState struct { Conflicts []commands.Conflict EditHistory *stack.Stack Platform commands.Platform + Updating bool } // NewGui builds a new gui handler @@ -140,6 +141,13 @@ func max(a, b int) int { return b } +func (gui *Gui) setAppStatus(status string) error { + if err := gui.renderString(gui.g, "appStatus", status); err != nil { + return err + } + return nil +} + // layout is called for every screen re-render e.g. when the screen is resized func (gui *Gui) layout(g *gocui.Gui) error { g.Highlight = true @@ -153,6 +161,12 @@ func (gui *Gui) layout(g *gocui.Gui) error { minimumWidth := 10 version := gui.Config.GetVersion() + appStatusView, _ := g.View("appStatus") + appStatusOptionsBoundary := -2 + if appStatusView != nil && len(appStatusView.Buffer()) > 2 { + appStatusOptionsBoundary = len(appStatusView.Buffer()) + 2 + } + panelSpacing := 1 if OverlappingEdges { panelSpacing = 0 @@ -230,7 +244,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { v.FgColor = gocui.ColorWhite } - if v, err := g.SetView("options", -1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil { + if v, err := g.SetView("options", appStatusOptionsBoundary-1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil { if err != gocui.ErrUnknownView { return err } @@ -253,6 +267,20 @@ func (gui *Gui) layout(g *gocui.Gui) error { } } + appStatusLeft := -1 + if appStatusOptionsBoundary < 2 { + appStatusLeft = -5 + } + + if v, err := g.SetView("appStatus", appStatusLeft, optionsTop, appStatusOptionsBoundary, optionsTop+2, 0); err != nil { + if err != gocui.ErrUnknownView { + return err + } + v.BgColor = gocui.ColorDefault + v.FgColor = gocui.ColorCyan + v.Frame = false + } + if v, err := g.SetView("version", width-len(version)-1, optionsTop, width, optionsTop+2, 0); err != nil { if err != gocui.ErrUnknownView { return err @@ -264,18 +292,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } - newVersion, err := gui.Updater.CheckForNewUpdate() - if err != nil { - return err - } - gui.Updater.NewVersion = "v0.1.75" - newVersion = "v0.1.75" - if newVersion != "" { - if err := gui.Updater.Update(); err != nil { - panic(err) - return err - } - } + // gui.Updater.CheckForNewUpdate(gui.onUpdateCheckFinish) // these are only called once gui.handleFileSelect(g, filesView) @@ -293,6 +310,36 @@ func (gui *Gui) layout(g *gocui.Gui) error { return nil } +func (gui *Gui) onUpdateFinish(err error) error { + gui.State.Updating = false + gui.setAppStatus("") + if err != nil { + gui.createErrorPanel(gui.g, "Update failed: "+err.Error()) + } + // TODO: on attempted quit, if downloading is true, ask if sure the user wants to quit + return nil +} + +func (gui *Gui) onUpdateCheckFinish(newVersion string, err error) error { + newVersion = "v0.1.72" + if err != nil { + // ignoring the error for now + return nil + } + if newVersion == "" { + return nil + } + title := "New version available!" + message := "Download latest version? (enter/esc)" + // TODO: support nil view in createConfirmationPanel or always go back to filesPanel when there is no previous view + return gui.createConfirmationPanel(gui.g, nil, title, message, func(g *gocui.Gui, v *gocui.View) error { + gui.State.Updating = true + gui.setAppStatus("updating...") + gui.Updater.Update(newVersion, gui.onUpdateFinish) + return nil + }, nil) // TODO: set config value saying not to check for another while if user hits escape +} + func (gui *Gui) fetch(g *gocui.Gui) error { gui.GitCommand.Fetch() gui.refreshStatus(g) @@ -300,11 +347,17 @@ func (gui *Gui) fetch(g *gocui.Gui) error { } func (gui *Gui) updateLoader(g *gocui.Gui) error { - if confirmationView, _ := g.View("confirmation"); confirmationView != nil { - content := gui.trimmedContent(confirmationView) - if strings.Contains(content, "...") { - staticContent := strings.Split(content, "...")[0] + "..." - gui.renderString(g, "confirmation", staticContent+" "+gui.loader()) + viewNames := []string{"confirmation", "appStatus"} + for _, viewName := range viewNames { + if view, _ := g.View(viewName); view != nil { + content := gui.trimmedContent(view) + if strings.Contains(content, "...") { + staticContent := strings.Split(content, "...")[0] + "..." + if viewName == "appStatus" { + // panic(staticContent + " " + gui.loader()) + } + gui.renderString(g, viewName, staticContent+" "+gui.loader()) + } } } return nil @@ -334,9 +387,6 @@ func (gui *Gui) Run() error { } defer g.Close() - // TODO: do this more elegantly - gui.Updater.CheckForNewUpdate() - gui.g = g // TODO: always use gui.g rather than passing g around everywhere if err := gui.SetColorScheme(); err != nil { @@ -381,6 +431,17 @@ func (gui *Gui) RunWithSubprocesses() { } } +func (gui *Gui) createUpdateQuitConfirmation(g *gocui.Gui, v *gocui.View) error { + title := "Currently Updating" + message := "An update is in progress. Are you sure you want to quit?" + return gui.createConfirmationPanel(gui.g, v, title, message, func(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit + }, nil) +} + func (gui *Gui) quit(g *gocui.Gui, v *gocui.View) error { + if gui.State.Updating { + return gui.createUpdateQuitConfirmation(g, v) + } return gocui.ErrQuit } diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 46fc79e40..cfe985867 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -108,7 +108,11 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error { previousView, err := g.View(gui.State.PreviousView) if err != nil { - panic(err) + // always fall back to files view if there's no 'previous' view stored + previousView, err = g.View("files") + if err != nil { + gui.Log.Error(err) + } } return gui.switchFocus(g, v, previousView) } @@ -216,6 +220,9 @@ func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error { if err != nil { return nil } + if viewName == "appStatus" { + gui.Log.Info(s) + } v.Clear() fmt.Fprint(v, s) v.Wrap = true diff --git a/pkg/updates/updates.go b/pkg/updates/updates.go index 3599808fb..dce62bdb4 100644 --- a/pkg/updates/updates.go +++ b/pkg/updates/updates.go @@ -2,7 +2,6 @@ package updates import ( "encoding/json" - "errors" "fmt" "io/ioutil" "net/http" @@ -10,11 +9,12 @@ import ( "os" "path/filepath" "runtime" + "time" "github.com/kardianos/osext" "github.com/Sirupsen/logrus" - getter "github.com/hashicorp/go-getter" + getter "github.com/jesseduffield/go-getter" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" ) @@ -22,9 +22,8 @@ import ( // Update checks for updates and does updates type Updater struct { LastChecked string - Log *logrus.Logger + Log *logrus.Entry Config config.AppConfigurer - NewVersion string OSCommand *commands.OSCommand } @@ -40,10 +39,11 @@ var ( // NewUpdater creates a new updater func NewUpdater(log *logrus.Logger, config config.AppConfigurer, osCommand *commands.OSCommand) (*Updater, error) { + contextLogger := log.WithField("context", "updates") updater := &Updater{ LastChecked: "today", - Log: log, + Log: contextLogger, Config: config, OSCommand: osCommand, } @@ -51,6 +51,7 @@ func NewUpdater(log *logrus.Logger, config config.AppConfigurer, osCommand *comm } func (u *Updater) getLatestVersionNumber() (string, error) { + time.Sleep(5) req, err := http.NewRequest("GET", projectUrl+"/releases/latest", nil) if err != nil { return "", err @@ -75,27 +76,45 @@ func (u *Updater) getLatestVersionNumber() (string, error) { return dat["tag_name"].(string), nil } -// CheckForNewUpdate checks if there is an available update -func (u *Updater) CheckForNewUpdate() (string, error) { +func (u *Updater) checkForNewUpdate() (string, error) { u.Log.Info("Checking for an updated version") - if u.Config.GetVersion() == "unversioned" { - u.Log.Info("Current version is not built from an official release so we won't check for an update") - return "", nil - } + // if u.Config.GetVersion() == "unversioned" { + // u.Log.Info("Current version is not built from an official release so we won't check for an update") + // return "", nil + // } newVersion, err := u.getLatestVersionNumber() if err != nil { return "", err } - u.NewVersion = newVersion u.Log.Info("Current version is " + u.Config.GetVersion()) u.Log.Info("New version is " + newVersion) - if newVersion == u.Config.GetVersion() { + // if newVersion == u.Config.GetVersion() { + // return "", nil + // } + + rawUrl, err := u.getBinaryUrl(newVersion) + if err != nil { + return "", err + } + u.Log.Info("Checking for resource at url " + rawUrl) + if !u.verifyResourceFound(rawUrl) { + u.Log.Error("Resource not found") return "", nil } - // TODO: verify here that there is a binary available for this OS/arch + u.Log.Info("Verified resource is available, ready to update") return newVersion, nil } +// CheckForNewUpdate checks if there is an available update +func (u *Updater) CheckForNewUpdate(onFinish func(string, error) error) { + go func() { + newVersion, err := u.checkForNewUpdate() + if err = onFinish(newVersion, err); err != nil { + u.Log.Error(err) + } + }() +} + func (u *Updater) mappedOs(os string) string { osMap := map[string]string{ "darwin": "Darwin", @@ -122,10 +141,7 @@ func (u *Updater) mappedArch(arch string) string { } // example: https://github.com/jesseduffield/lazygit/releases/download/v0.1.73/lazygit_0.1.73_Darwin_x86_64.tar.gz -func (u *Updater) getBinaryUrl() (string, error) { - if u.NewVersion == "" { - return "", errors.New("Must run CheckForUpdate() before running getBinaryUrl() to get the new version number") - } +func (u *Updater) getBinaryUrl(newVersion string) (string, error) { extension := "tar.gz" if runtime.GOOS == "windows" { extension = "zip" @@ -133,8 +149,8 @@ func (u *Updater) getBinaryUrl() (string, error) { url := fmt.Sprintf( "%s/releases/download/%s/lazygit_%s_%s_%s.%s", projectUrl, - u.NewVersion, - u.NewVersion[1:], + newVersion, + newVersion[1:], u.mappedOs(runtime.GOOS), u.mappedArch(runtime.GOARCH), extension, @@ -143,54 +159,79 @@ func (u *Updater) getBinaryUrl() (string, error) { return url, nil } -func (u *Updater) Update() error { - rawUrl, err := u.getBinaryUrl() +// Update downloads the latest binary and replaces the current binary with it +func (u *Updater) Update(newVersion string, onFinish func(error) error) { + go func() { + err := u.update(newVersion) + if err = onFinish(err); err != nil { + u.Log.Error(err) + } + }() +} + +func (u *Updater) update(newVersion string) error { + rawUrl, err := u.getBinaryUrl(newVersion) if err != nil { return err } + u.Log.Info("updating with url " + rawUrl) return u.downloadAndInstall(rawUrl) } func (u *Updater) downloadAndInstall(rawUrl string) error { url, err := url.Parse(rawUrl) if err != nil { - panic(err) + return err } g := new(getter.HttpGetter) tempDir, err := ioutil.TempDir("", "lazygit") if err != nil { - panic(err) + return err } defer os.RemoveAll(tempDir) + u.Log.Info("temp directory is " + tempDir) // Get it! if err := g.Get(tempDir, url); err != nil { - panic(err) + return err } - extension := "" - if runtime.GOOS == "windows" { - extension = ".exe" + // get the path of the current binary + binaryPath, err := osext.Executable() + if err != nil { + return err } + u.Log.Info("binary path is " + binaryPath) + + binaryName := filepath.Base(binaryPath) + u.Log.Info("binary name is " + binaryName) // Verify the main file exists - tempPath := filepath.Join(tempDir, "lazygit"+extension) + tempPath := filepath.Join(tempDir, binaryName) + u.Log.Info("temp path to binary is " + tempPath) if _, err := os.Stat(tempPath); err != nil { - panic(err) - } - - // get the path of the current binary - execPath, err := osext.Executable() - if err != nil { - panic(err) + return err } // swap out the old binary for the new one - err = os.Rename(tempPath, execPath+"2") + err = os.Rename(tempPath, binaryPath) if err != nil { - panic(err) + return err } + u.Log.Info("update complete!") return nil } + +func (u *Updater) verifyResourceFound(rawUrl string) bool { + resp, err := http.Head(rawUrl) + if err != nil { + return false + } + defer resp.Body.Close() + u.Log.Info("Received status code ", resp.StatusCode) + // 403 means the resource is there (not going to bother adding extra request headers) + // 404 means its not + return resp.StatusCode == 403 +} |