1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-05 15:15:49 +02:00

pkg/updates: Refactoring

This commit is contained in:
KimMachineGune 2019-01-10 07:42:05 +09:00 committed by Jesse Duffield
parent 1db8801771
commit 7ff022f1e7

View File

@ -36,25 +36,24 @@ type Updaterer interface {
Update() Update()
} }
var ( const (
projectUrl = "https://github.com/jesseduffield/lazygit" PROJECT_URL = "https://github.com/jesseduffield/lazygit"
) )
// NewUpdater creates a new updater // NewUpdater creates a new updater
func NewUpdater(log *logrus.Entry, config config.AppConfigurer, osCommand *commands.OSCommand, tr *i18n.Localizer) (*Updater, error) { func NewUpdater(log *logrus.Entry, config config.AppConfigurer, osCommand *commands.OSCommand, tr *i18n.Localizer) (*Updater, error) {
contextLogger := log.WithField("context", "updates") contextLogger := log.WithField("context", "updates")
updater := &Updater{ return &Updater{
Log: contextLogger, Log: contextLogger,
Config: config, Config: config,
OSCommand: osCommand, OSCommand: osCommand,
Tr: tr, Tr: tr,
} }, nil
return updater, nil
} }
func (u *Updater) getLatestVersionNumber() (string, error) { func (u *Updater) getLatestVersionNumber() (string, error) {
req, err := http.NewRequest("GET", projectUrl+"/releases/latest", nil) req, err := http.NewRequest("GET", PROJECT_URL+"/releases/latest", nil)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -65,17 +64,16 @@ func (u *Updater) getLatestVersionNumber() (string, error) {
return "", err return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil { dec := json.NewDecoder(resp.Body)
data := struct {
TagName string `json:"tag_name"`
}{}
if err := dec.Decode(&data); err != nil {
return "", err return "", err
} }
byt := []byte(body) return data.TagName, nil
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
return "", err
}
return dat["tag_name"].(string), nil
} }
// RecordLastUpdateCheck records last time an update check was performed // RecordLastUpdateCheck records last time an update check was performed
@ -225,14 +223,14 @@ func (u *Updater) getBinaryUrl(newVersion string) (string, error) {
} }
url := fmt.Sprintf( url := fmt.Sprintf(
"%s/releases/download/%s/lazygit_%s_%s_%s.%s", "%s/releases/download/%s/lazygit_%s_%s_%s.%s",
projectUrl, PROJECT_URL,
newVersion, newVersion,
newVersion[1:], newVersion[1:],
u.mappedOs(runtime.GOOS), u.mappedOs(runtime.GOOS),
u.mappedArch(runtime.GOARCH), u.mappedArch(runtime.GOARCH),
extension, extension,
) )
u.Log.Info("url for latest release is " + url) u.Log.Info("Url for latest release is " + url)
return url, nil return url, nil
} }
@ -251,7 +249,7 @@ func (u *Updater) update(newVersion string) error {
if err != nil { if err != nil {
return err return err
} }
u.Log.Info("updating with url " + rawUrl) u.Log.Info("Updating with url " + rawUrl)
return u.downloadAndInstall(rawUrl) return u.downloadAndInstall(rawUrl)
} }
@ -267,7 +265,7 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
return err return err
} }
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
u.Log.Info("temp directory is " + tempDir) u.Log.Info("Temp directory is " + tempDir)
// Get it! // Get it!
if err := g.Get(tempDir, url); err != nil { if err := g.Get(tempDir, url); err != nil {
@ -279,14 +277,14 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
if err != nil { if err != nil {
return err return err
} }
u.Log.Info("binary path is " + binaryPath) u.Log.Info("Binary path is " + binaryPath)
binaryName := filepath.Base(binaryPath) binaryName := filepath.Base(binaryPath)
u.Log.Info("binary name is " + binaryName) u.Log.Info("Binary name is " + binaryName)
// Verify the main file exists // Verify the main file exists
tempPath := filepath.Join(tempDir, binaryName) tempPath := filepath.Join(tempDir, binaryName)
u.Log.Info("temp path to binary is " + tempPath) u.Log.Info("Temp path to binary is " + tempPath)
if _, err := os.Stat(tempPath); err != nil { if _, err := os.Stat(tempPath); err != nil {
return err return err
} }
@ -296,7 +294,7 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
if err != nil { if err != nil {
return err return err
} }
u.Log.Info("update complete!") u.Log.Info("Update complete!")
return nil return nil
} }