feat: fix permission problem of temp dirs

This commit is contained in:
Ryooooooga 2022-04-01 23:38:41 +09:00 committed by Jesse Duffield
parent 2fbb52fa2c
commit 86c259623c
7 changed files with 26 additions and 12 deletions

View file

@ -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())
}

View file

@ -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 {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 {

View file

@ -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