1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/updates/updates.go

284 lines
6.5 KiB
Go
Raw Normal View History

2018-08-18 11:54:44 +02:00
package updates
2018-08-19 15:28:29 +02:00
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
2018-08-25 07:55:49 +02:00
"strings"
2018-08-23 10:43:16 +02:00
"time"
2018-08-19 15:28:29 +02:00
"github.com/kardianos/osext"
2018-08-23 10:43:16 +02:00
getter "github.com/jesseduffield/go-getter"
2018-08-19 15:28:29 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/sirupsen/logrus"
2018-08-19 15:28:29 +02:00
)
2018-08-18 11:54:44 +02:00
// Update checks for updates and does updates
2018-08-19 15:28:29 +02:00
type Updater struct {
2018-08-25 07:55:49 +02:00
Log *logrus.Entry
Config config.AppConfigurer
OSCommand *commands.OSCommand
2018-08-18 11:54:44 +02:00
}
// Updater implements the check and update methods
2018-08-19 15:28:29 +02:00
type Updaterer interface {
CheckForNewUpdate()
2018-08-18 11:54:44 +02:00
Update()
}
2018-08-19 15:28:29 +02:00
var (
projectUrl = "https://github.com/jesseduffield/lazygit"
)
2018-08-18 11:54:44 +02:00
// NewUpdater creates a new updater
2018-08-19 15:28:29 +02:00
func NewUpdater(log *logrus.Logger, config config.AppConfigurer, osCommand *commands.OSCommand) (*Updater, error) {
2018-08-23 10:43:16 +02:00
contextLogger := log.WithField("context", "updates")
2018-08-18 11:54:44 +02:00
2018-08-19 15:28:29 +02:00
updater := &Updater{
2018-08-25 07:55:49 +02:00
Log: contextLogger,
Config: config,
OSCommand: osCommand,
2018-08-19 15:28:29 +02:00
}
return updater, nil
}
func (u *Updater) getLatestVersionNumber() (string, error) {
req, err := http.NewRequest("GET", projectUrl+"/releases/latest", nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
byt := []byte(body)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
return "", err
2018-08-18 11:54:44 +02:00
}
2018-08-19 15:28:29 +02:00
return dat["tag_name"].(string), nil
2018-08-18 11:54:44 +02:00
}
2018-08-25 07:55:49 +02:00
func (u *Updater) RecordLastUpdateCheck() error {
u.Config.GetAppState().LastUpdateCheck = time.Now().Unix()
return u.Config.SaveAppState()
}
// expecting version to be of the form `v12.34.56`
func (u *Updater) majorVersionDiffers(oldVersion, newVersion string) bool {
return strings.Split(oldVersion, ".")[0] != strings.Split(newVersion, ".")[0]
}
2018-08-23 10:43:16 +02:00
func (u *Updater) checkForNewUpdate() (string, error) {
2018-08-19 15:28:29 +02:00
u.Log.Info("Checking for an updated version")
2018-08-25 07:55:49 +02:00
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
}
2018-08-19 15:28:29 +02:00
newVersion, err := u.getLatestVersionNumber()
if err != nil {
return "", err
}
u.Log.Info("Current version is " + u.Config.GetVersion())
u.Log.Info("New version is " + newVersion)
2018-08-25 07:55:49 +02:00
if err := u.RecordLastUpdateCheck(); err != nil {
return "", err
}
if newVersion == u.Config.GetVersion() {
return "", nil
}
if u.majorVersionDiffers(u.Config.GetVersion(), newVersion) {
u.Log.Info("New version has non-backwards compatible changes.")
return "", nil
}
2018-08-23 10:43:16 +02:00
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")
2018-08-19 15:28:29 +02:00
return "", nil
}
2018-08-23 10:43:16 +02:00
u.Log.Info("Verified resource is available, ready to update")
2018-08-25 07:55:49 +02:00
2018-08-19 15:28:29 +02:00
return newVersion, nil
}
2018-08-23 10:43:16 +02:00
// CheckForNewUpdate checks if there is an available update
2018-08-25 07:55:49 +02:00
func (u *Updater) CheckForNewUpdate(onFinish func(string, error) error, userRequested bool) {
if !userRequested && u.skipUpdateCheck() {
return
}
2018-08-23 10:43:16 +02:00
go func() {
newVersion, err := u.checkForNewUpdate()
if err = onFinish(newVersion, err); err != nil {
u.Log.Error(err)
}
}()
}
2018-08-25 07:55:49 +02:00
func (u *Updater) skipUpdateCheck() bool {
2018-08-27 11:37:01 +02:00
// will remove the check for windows after adding a manifest file asking for
// the required permissions
if runtime.GOOS == "windows" {
return true
}
2018-08-25 07:55:49 +02:00
if u.Config.GetBuildSource() != "buildBinary" {
return true
}
userConfig := u.Config.GetUserConfig()
if userConfig.Get("update.method") == "never" {
return true
}
currentTimestamp := time.Now().Unix()
lastUpdateCheck := u.Config.GetAppState().LastUpdateCheck
days := userConfig.GetInt64("update.days")
return (currentTimestamp-lastUpdateCheck)/(60*60*24) < days
}
2018-08-19 15:28:29 +02:00
func (u *Updater) mappedOs(os string) string {
osMap := map[string]string{
"darwin": "Darwin",
"linux": "Linux",
"windows": "Windows",
}
result, found := osMap[os]
if found {
return result
}
return os
}
func (u *Updater) mappedArch(arch string) string {
archMap := map[string]string{
"386": "32-bit",
"amd64": "x86_64",
}
result, found := archMap[arch]
if found {
return result
}
return arch
}
// example: https://github.com/jesseduffield/lazygit/releases/download/v0.1.73/lazygit_0.1.73_Darwin_x86_64.tar.gz
2018-08-23 10:43:16 +02:00
func (u *Updater) getBinaryUrl(newVersion string) (string, error) {
2018-08-19 15:28:29 +02:00
extension := "tar.gz"
if runtime.GOOS == "windows" {
extension = "zip"
}
url := fmt.Sprintf(
"%s/releases/download/%s/lazygit_%s_%s_%s.%s",
projectUrl,
2018-08-23 10:43:16 +02:00
newVersion,
newVersion[1:],
2018-08-19 15:28:29 +02:00
u.mappedOs(runtime.GOOS),
u.mappedArch(runtime.GOARCH),
extension,
)
u.Log.Info("url for latest release is " + url)
return url, nil
}
2018-08-23 10:43:16 +02:00
// 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)
2018-08-19 15:28:29 +02:00
if err != nil {
return err
}
2018-08-23 10:43:16 +02:00
u.Log.Info("updating with url " + rawUrl)
2018-08-19 15:28:29 +02:00
return u.downloadAndInstall(rawUrl)
}
func (u *Updater) downloadAndInstall(rawUrl string) error {
url, err := url.Parse(rawUrl)
if err != nil {
2018-08-23 10:43:16 +02:00
return err
2018-08-19 15:28:29 +02:00
}
g := new(getter.HttpGetter)
tempDir, err := ioutil.TempDir("", "lazygit")
if err != nil {
2018-08-23 10:43:16 +02:00
return err
2018-08-19 15:28:29 +02:00
}
defer os.RemoveAll(tempDir)
2018-08-23 10:43:16 +02:00
u.Log.Info("temp directory is " + tempDir)
2018-08-19 15:28:29 +02:00
// Get it!
if err := g.Get(tempDir, url); err != nil {
2018-08-23 10:43:16 +02:00
return err
2018-08-19 15:28:29 +02:00
}
2018-08-23 10:43:16 +02:00
// get the path of the current binary
binaryPath, err := osext.Executable()
if err != nil {
return err
2018-08-19 15:28:29 +02:00
}
2018-08-23 10:43:16 +02:00
u.Log.Info("binary path is " + binaryPath)
binaryName := filepath.Base(binaryPath)
u.Log.Info("binary name is " + binaryName)
2018-08-19 15:28:29 +02:00
// Verify the main file exists
2018-08-23 10:43:16 +02:00
tempPath := filepath.Join(tempDir, binaryName)
u.Log.Info("temp path to binary is " + tempPath)
2018-08-19 15:28:29 +02:00
if _, err := os.Stat(tempPath); err != nil {
2018-08-23 10:43:16 +02:00
return err
2018-08-19 15:28:29 +02:00
}
// swap out the old binary for the new one
2018-08-23 10:43:16 +02:00
err = os.Rename(tempPath, binaryPath)
2018-08-19 15:28:29 +02:00
if err != nil {
2018-08-23 10:43:16 +02:00
return err
2018-08-19 15:28:29 +02:00
}
2018-08-23 10:43:16 +02:00
u.Log.Info("update complete!")
2018-08-18 11:54:44 +02:00
2018-08-19 15:28:29 +02:00
return nil
2018-08-18 11:54:44 +02:00
}
2018-08-23 10:43:16 +02:00
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
}