1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-08 23:56:15 +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

@ -13,15 +13,16 @@ import (
// AppConfig contains the base configuration fields required for lazygit. // AppConfig contains the base configuration fields required for lazygit.
type AppConfig struct { type AppConfig struct {
Debug bool `long:"debug" env:"DEBUG" default:"false"` Debug bool `long:"debug" env:"DEBUG" default:"false"`
Version string `long:"version" env:"VERSION" default:"unversioned"` Version string `long:"version" env:"VERSION" default:"unversioned"`
Commit string `long:"commit" env:"COMMIT"` Commit string `long:"commit" env:"COMMIT"`
BuildDate string `long:"build-date" env:"BUILD_DATE"` BuildDate string `long:"build-date" env:"BUILD_DATE"`
Name string `long:"name" env:"NAME" default:"lazygit"` Name string `long:"name" env:"NAME" default:"lazygit"`
BuildSource string `long:"build-source" env:"BUILD_SOURCE" default:""` BuildSource string `long:"build-source" env:"BUILD_SOURCE" default:""`
UserConfig *viper.Viper UserConfig *viper.Viper
AppState *AppState UserConfigPath string
IsNewRepo bool AppState *AppState
IsNewRepo bool
} }
// AppConfigurer interface allows individual app config structs to inherit Fields // AppConfigurer interface allows individual app config structs to inherit Fields
@ -34,6 +35,7 @@ type AppConfigurer interface {
GetName() string GetName() string
GetBuildSource() string GetBuildSource() string
GetUserConfig() *viper.Viper GetUserConfig() *viper.Viper
GetUserConfigPath() string
GetAppState() *AppState GetAppState() *AppState
WriteToUserConfig(string, string) error WriteToUserConfig(string, string) error
SaveAppState() error SaveAppState() error
@ -44,7 +46,7 @@ type AppConfigurer interface {
// NewAppConfig makes a new app config // NewAppConfig makes a new app config
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -54,15 +56,16 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
} }
appConfig := &AppConfig{ appConfig := &AppConfig{
Name: "lazygit", Name: "lazygit",
Version: version, Version: version,
Commit: commit, Commit: commit,
BuildDate: date, BuildDate: date,
Debug: debuggingFlag, Debug: debuggingFlag,
BuildSource: buildSource, BuildSource: buildSource,
UserConfig: userConfig, UserConfig: userConfig,
AppState: &AppState{}, UserConfigPath: userConfigPath,
IsNewRepo: false, AppState: &AppState{},
IsNewRepo: false,
} }
if err := appConfig.LoadAppState(); err != nil { if err := appConfig.LoadAppState(); err != nil {
@ -123,6 +126,10 @@ func (c *AppConfig) GetAppState() *AppState {
return c.AppState return c.AppState
} }
func (c *AppConfig) GetUserConfigPath() string {
return c.UserConfigPath
}
func newViper(filename string) (*viper.Viper, error) { func newViper(filename string) (*viper.Viper, error) {
v := viper.New() v := viper.New()
v.SetConfigType("yaml") v.SetConfigType("yaml")
@ -131,23 +138,24 @@ func newViper(filename string) (*viper.Viper, error) {
} }
// LoadConfig gets the user's config // 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) v, err := newViper(filename)
if err != nil { if err != nil {
return nil, err return nil, "", err
} }
if withDefaults { if withDefaults {
if err = LoadDefaults(v, GetDefaultConfig()); err != nil { if err = LoadDefaults(v, GetDefaultConfig()); err != nil {
return nil, err return nil, "", err
} }
if err = LoadDefaults(v, GetPlatformDefaultConfig()); err != nil { if err = LoadDefaults(v, GetPlatformDefaultConfig()); err != nil {
return nil, err return nil, "", err
} }
} }
if err = LoadAndMergeFile(v, filename+".yml"); err != nil { configPath, err := LoadAndMergeFile(v, filename+".yml")
return nil, err if err != nil {
return nil, "", err
} }
return v, nil return v, configPath, nil
} }
// LoadDefaults loads in the defaults defined in this file // 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 // LoadAndMergeFile Loads the config/state file, creating
// the file has an empty one if it does not exist // 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) configPath, err := prepareConfigFile(filename)
if err != nil { if err != nil {
return err return "", err
} }
v.AddConfigPath(filepath.Dir(configPath)) 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 // WriteToUserConfig adds a key/value pair to the user's config and saves it
func (c *AppConfig) WriteToUserConfig(key, value string) error { func (c *AppConfig) WriteToUserConfig(key, value string) error {
// reloading the user config directly (without defaults) so that we're not // reloading the user config directly (without defaults) so that we're not
// writing any defaults back to the user's config // writing any defaults back to the user's config
v, err := LoadConfig("config", false) v, _, err := LoadConfig("config", false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,7 +3,6 @@ package updates
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -16,7 +15,7 @@ import (
"github.com/kardianos/osext" "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/commands"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
@ -261,15 +260,11 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
} }
g := new(getter.HttpGetter) g := new(getter.HttpGetter)
tempDir, err := ioutil.TempDir("", "lazygit") configDir := filepath.Dir(u.Config.GetUserConfigPath())
if err != nil { u.Log.Info("Download directory is " + configDir)
return err
}
defer os.RemoveAll(tempDir)
u.Log.Info("Temp directory is " + tempDir)
// Get it! // Get it!
if err := g.Get(tempDir, url); err != nil { if err := g.Get(configDir, url); err != nil {
return err return err
} }
@ -284,7 +279,7 @@ func (u *Updater) downloadAndInstall(rawUrl string) error {
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(configDir, 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