diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2022-04-01 23:38:41 +0900 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-04-02 08:48:38 +1100 |
commit | 86c259623c0f54b5eac6022cbdf232521de19ab8 (patch) | |
tree | e88a06d4f261da5f9ba8d5594e25b639a06af095 | |
parent | 2fbb52fa2cc1eaed9952206b0091b9ac2ee63d8b (diff) |
feat: fix permission problem of temp dirs
-rw-r--r-- | main.go | 8 | ||||
-rw-r--r-- | pkg/app/app.go | 2 | ||||
-rw-r--r-- | pkg/commands/git_commands/working_tree.go | 2 | ||||
-rw-r--r-- | pkg/commands/oscommands/dummies.go | 5 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 10 | ||||
-rw-r--r-- | pkg/config/app_config.go | 9 | ||||
-rw-r--r-- | pkg/gui/gui.go | 2 |
7 files changed, 26 insertions, 12 deletions
@@ -127,7 +127,13 @@ func main() { } } - appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag) + tempDir, err := os.MkdirTemp("", "lazygit-*") + if err != nil { + log.Fatal(err.Error()) + } + defer os.RemoveAll(tempDir) + + appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag, tempDir) if err != nil { log.Fatal(err.Error()) } diff --git a/pkg/app/app.go b/pkg/app/app.go index c81332df0..592505d60 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -122,7 +122,7 @@ func NewApp(config config.AppConfigurer) (*App, error) { return app, nil } - app.OSCommand = oscommands.NewOSCommand(app.Common, oscommands.GetPlatform(), oscommands.NewNullGuiIO(log)) + app.OSCommand = oscommands.NewOSCommand(app.Common, config, oscommands.GetPlatform(), oscommands.NewNullGuiIO(log)) app.Updater, err = updates.NewUpdater(app.Common, config, app.OSCommand) if err != nil { diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 08e247459..d9aecf01f 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -251,7 +251,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain } func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error { - filepath := filepath.Join(oscommands.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") + filepath := filepath.Join(self.os.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") self.Log.Infof("saving temporary patch to %s", filepath) if err := self.os.CreateFileWithContent(filepath, patch); err != nil { return err diff --git a/pkg/commands/oscommands/dummies.go b/pkg/commands/oscommands/dummies.go index 158e9a9c1..b5978e4b5 100644 --- a/pkg/commands/oscommands/dummies.go +++ b/pkg/commands/oscommands/dummies.go @@ -2,12 +2,13 @@ package oscommands import ( "github.com/jesseduffield/lazygit/pkg/common" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) // NewDummyOSCommand creates a new dummy OSCommand for testing func NewDummyOSCommand() *OSCommand { - osCmd := NewOSCommand(utils.NewDummyCommon(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) + osCmd := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) return osCmd } @@ -56,7 +57,7 @@ var dummyPlatform = &Platform{ } func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand { - osCommand := NewOSCommand(utils.NewDummyCommon(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) + osCommand := NewOSCommand(utils.NewDummyCommon(), config.NewDummyAppConfig(), dummyPlatform, NewNullGuiIO(utils.NewDummyLog())) osCommand.Cmd = NewDummyCmdObjBuilder(runner) return osCommand diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index a9c38e482..702aca783 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -14,6 +14,7 @@ import ( "github.com/atotto/clipboard" "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/common" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -27,6 +28,8 @@ type OSCommand struct { removeFileFn func(string) error Cmd *CmdObjBuilder + + tempDir string } // Platform stores the os state @@ -39,13 +42,14 @@ type Platform struct { } // NewOSCommand os command runner -func NewOSCommand(common *common.Common, platform *Platform, guiIO *guiIO) *OSCommand { +func NewOSCommand(common *common.Common, config config.AppConfigurer, platform *Platform, guiIO *guiIO) *OSCommand { c := &OSCommand{ Common: common, Platform: platform, getenvFn: os.Getenv, removeFileFn: os.RemoveAll, guiIO: guiIO, + tempDir: config.GetTempDir(), } runner := &cmdObjRunner{log: common.Log, guiIO: guiIO} @@ -236,8 +240,8 @@ func (c *OSCommand) Getenv(key string) string { return c.getenvFn(key) } -func GetTempDir() string { - return filepath.Join(os.TempDir(), "lazygit") +func (c *OSCommand) GetTempDir() string { + return c.tempDir } // GetLazygitPath returns the path of the currently executed file diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 40509e86a..e43ea41a7 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -43,13 +43,14 @@ type AppConfigurer interface { GetUserConfigPaths() []string GetUserConfigDir() string ReloadUserConfig() error + GetTempDir() string GetAppState() *AppState SaveAppState() error } // 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, tempDir string) (*AppConfig, error) { configDir, err := findOrCreateConfigDir() if err != nil && !os.IsPermission(err) { return nil, err @@ -74,8 +75,6 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg debuggingFlag = true } - tempDir := filepath.Join(os.TempDir(), "lazygit") - appState, err := loadAppState() if err != nil { return nil, err @@ -221,6 +220,10 @@ func (c *AppConfig) ReloadUserConfig() error { return nil } +func (c *AppConfig) GetTempDir() string { + return c.TempDir +} + func configFilePath(filename string) (string, error) { folder, err := findOrCreateConfigDir() if err != nil { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index caa1a6afe..1af94088e 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -470,7 +470,7 @@ func NewGui( credentialsHelper.PromptUserForCredential, ) - osCommand := oscommands.NewOSCommand(cmn, oscommands.GetPlatform(), guiIO) + osCommand := oscommands.NewOSCommand(cmn, config, oscommands.GetPlatform(), guiIO) gui.os = osCommand |