summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDawid Dziurla <dawidd0811@gmail.com>2019-09-15 10:49:44 +0200
committerJesse Duffield <jessedduffield@gmail.com>2019-09-24 19:01:40 +1000
commit0d25d113c9a64be71e1a1a6756a22efc6a35d01d (patch)
tree3da8456385c5a641db3dda10385fff16f07c8d40 /pkg
parent7c70913e8dac9326bcd96b98755de11dd61cfd76 (diff)
download updated binary to config dir rather than /tmp
Diffstat (limited to 'pkg')
-rw-r--r--pkg/config/app_config.go68
-rw-r--r--pkg/updates/updates.go15
2 files changed, 43 insertions, 40 deletions
diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go
index aefd86896..a91ed89ee 100644
--- a/pkg/config/app_config.go
+++ b/pkg/config/app_config.go
@@ -13,15 +13,16 @@ import (
// AppConfig contains the base configuration fields required for lazygit.
type AppConfig struct {
- Debug bool `long:"debug" env:"DEBUG" default:"false"`
- Version string `long:"version" env:"VERSION" default:"unversioned"`
- Commit string `long:"commit" env:"COMMIT"`
- BuildDate string `long:"build-date" env:"BUILD_DATE"`
- Name string `long:"name" env:"NAME" default:"lazygit"`
- BuildSource string `long:"build-source" env:"BUILD_SOURCE" default:""`
- UserConfig *viper.Viper
- AppState *AppState
- IsNewRepo bool
+ Debug bool `long:"debug" env:"DEBUG" default:"false"`
+ Version string `long:"version" env:"VERSION" default:"unversioned"`
+ Commit string `long:"commit" env:"COMMIT"`
+ BuildDate string `long:"build-date" env:"BUILD_DATE"`
+ 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
}
// AppConfigurer interface allows individual app config structs to inherit Fields
@@ -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
}
@@ -54,15 +56,16 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg
}
appConfig := &AppConfig{
- Name: "lazygit",
- Version: version,
- Commit: commit,
- BuildDate: date,
- Debug: debuggingFlag,
- BuildSource: buildSource,
- UserConfig: userConfig,
- AppState: &AppState{},
- IsNewRepo: false,
+ Name: "lazygit",
+ Version: version,
+ Commit: commit,
+ BuildDate: date,
+ Debug: debuggingFlag,
+ BuildSource: buildSource,
+ UserConfig: userConfig,
+ UserConfigPath: userConfigPath,
+ AppState: &AppState{},
+ IsNewRepo: false,
}
if err := appConfig.LoadAppState(); err != nil {
@@ -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
}
diff --git a/pkg/updates/updates.go b/pkg/updates/updates.go
index a4ec67e27..60ba31882 100644
--- a/pkg/updates/updates.go
+++ b/pkg/updates/updates.go
@@ -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