1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-13 00:07:59 +02:00

download updated binary to config dir rather than /tmp

This commit is contained in:
Dawid Dziurla 2019-09-15 10:49:44 +02:00 committed by Jesse Duffield
parent 7c70913e8d
commit 0d25d113c9
2 changed files with 43 additions and 40 deletions

View File

@ -20,6 +20,7 @@ type AppConfig struct {
Name string `long:"name" env:"NAME" default:"lazygit"`
BuildSource string `long:"build-source" env:"BUILD_SOURCE" default:""`
UserConfig *viper.Viper
UserConfigPath string
AppState *AppState
IsNewRepo bool
}
@ -34,6 +35,7 @@ type AppConfigurer interface {
GetName() string
GetBuildSource() string
GetUserConfig() *viper.Viper
GetUserConfigPath() string
GetAppState() *AppState
WriteToUserConfig(string, string) error
SaveAppState() error
@ -44,7 +46,7 @@ type AppConfigurer interface {
// NewAppConfig makes a new app config
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) {
userConfig, err := LoadConfig("config", true)
userConfig, userConfigPath, err := LoadConfig("config", true)
if err != nil {
return nil, err
}
@ -61,6 +63,7 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
Debug: debuggingFlag,
BuildSource: buildSource,
UserConfig: userConfig,
UserConfigPath: userConfigPath,
AppState: &AppState{},
IsNewRepo: false,
}
@ -123,6 +126,10 @@ func (c *AppConfig) GetAppState() *AppState {
return c.AppState
}
func (c *AppConfig) GetUserConfigPath() string {
return c.UserConfigPath
}
func newViper(filename string) (*viper.Viper, error) {
v := viper.New()
v.SetConfigType("yaml")
@ -131,23 +138,24 @@ func newViper(filename string) (*viper.Viper, error) {
}
// LoadConfig gets the user's config
func LoadConfig(filename string, withDefaults bool) (*viper.Viper, error) {
func LoadConfig(filename string, withDefaults bool) (*viper.Viper, string, error) {
v, err := newViper(filename)
if err != nil {
return nil, err
return nil, "", err
}
if withDefaults {
if err = LoadDefaults(v, GetDefaultConfig()); err != nil {
return nil, err
return nil, "", err
}
if err = LoadDefaults(v, GetPlatformDefaultConfig()); err != nil {
return nil, err
return nil, "", err
}
}
if err = LoadAndMergeFile(v, filename+".yml"); err != nil {
return nil, err
configPath, err := LoadAndMergeFile(v, filename+".yml")
if err != nil {
return nil, "", err
}
return v, nil
return v, configPath, nil
}
// LoadDefaults loads in the defaults defined in this file
@ -173,21 +181,21 @@ func prepareConfigFile(filename string) (string, error) {
// LoadAndMergeFile Loads the config/state file, creating
// the file has an empty one if it does not exist
func LoadAndMergeFile(v *viper.Viper, filename string) error {
func LoadAndMergeFile(v *viper.Viper, filename string) (string, error) {
configPath, err := prepareConfigFile(filename)
if err != nil {
return err
return "", err
}
v.AddConfigPath(filepath.Dir(configPath))
return v.MergeInConfig()
return configPath, v.MergeInConfig()
}
// WriteToUserConfig adds a key/value pair to the user's config and saves it
func (c *AppConfig) WriteToUserConfig(key, value string) error {
// reloading the user config directly (without defaults) so that we're not
// writing any defaults back to the user's config
v, err := LoadConfig("config", false)
v, _, err := LoadConfig("config", false)
if err != nil {
return err
}

View File

@ -3,7 +3,6 @@ package updates
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
@ -16,7 +15,7 @@ import (
"github.com/kardianos/osext"
getter "github.com/jesseduffield/go-getter"
"github.com/jesseduffield/go-getter"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n"
@ -261,15 +260,11 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
}
g := new(getter.HttpGetter)
tempDir, err := ioutil.TempDir("", "lazygit")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
u.Log.Info("Temp directory is " + tempDir)
configDir := filepath.Dir(u.Config.GetUserConfigPath())
u.Log.Info("Download directory is " + configDir)
// Get it!
if err := g.Get(tempDir, url); err != nil {
if err := g.Get(configDir, url); err != nil {
return err
}
@ -284,7 +279,7 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
u.Log.Info("Binary name is " + binaryName)
// Verify the main file exists
tempPath := filepath.Join(tempDir, binaryName)
tempPath := filepath.Join(configDir, binaryName)
u.Log.Info("Temp path to binary is " + tempPath)
if _, err := os.Stat(tempPath); err != nil {
return err