Skip to content

Commit

Permalink
Use $XDG_STATE_DIR for state.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
horriblename committed Dec 31, 2023
1 parent 4030f16 commit d5ed386
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,9 @@ func isCustomConfigFile(path string) bool {
}

func ConfigDir() string {
legacyConfigDirectory := configDirForVendor("jesseduffield")
if _, err := os.Stat(legacyConfigDirectory); !os.IsNotExist(err) {
return legacyConfigDirectory
}
configDirectory := configDirForVendor("")
return configDirectory
}
_, filePath := findConfigFile("config.yml")

func configDirForVendor(vendor string) string {
envConfigDir := os.Getenv("CONFIG_DIR")
if envConfigDir != "" {
return envConfigDir
}
configDirs := xdg.New(vendor, "lazygit")
return configDirs.ConfigHome()
return filepath.Dir(filePath)
}

func findOrCreateConfigDir() (string, error) {
Expand Down Expand Up @@ -245,16 +233,50 @@ func (c *AppConfig) GetTempDir() string {
}

func configFilePath(filename string) (string, error) {
folder, err := findOrCreateConfigDir()
if err != nil {
return "", err
found, path := findConfigFile(filename)

if found {
return path, nil
} else {
return path, os.MkdirAll(filepath.Dir(path), 0o755)
}
}

func findConfigFile(filename string) (found bool, path string) {
if envConfigDir := os.Getenv("CONFIG_DIR"); envConfigDir != "" {
return true, filepath.Join(envConfigDir, filename)
}

legacyConfigPath, err := xdg.SearchConfigFile(filepath.Join("jesseduffield", "lazygit", filename))
if err == nil {
return true, legacyConfigPath
}

return filepath.Join(folder, filename), nil
configFilepath, err := xdg.SearchConfigFile(filepath.Join("lazygit", filename))
if err == nil {
return true, configFilepath
}

return false, filepath.Join(xdg.ConfigHome, "lazygit")
}

var ConfigFilename = "config.yml"

func stateFilePath(filename string) (string, error) {
stateFile, err := xdg.SearchStateFile(filepath.Join("lazygit", filename))
if err == nil {
return stateFile, nil
}

found, legacyStateFile := findConfigFile(filename)

if found {
return legacyStateFile, nil
} else {
return xdg.StateFile(filepath.Join("lazygit", filename))
}
}

// ConfigFilename returns the filename of the default config file
func (c *AppConfig) ConfigFilename() string {
return filepath.Join(c.UserConfigDir, ConfigFilename)
Expand All @@ -267,7 +289,7 @@ func (c *AppConfig) SaveAppState() error {
return err
}

filepath, err := configFilePath("state.yml")
filepath, err := stateFilePath(stateFileName)
if err != nil {
return err
}
Expand All @@ -281,11 +303,13 @@ func (c *AppConfig) SaveAppState() error {
return err
}

var stateFileName = "state.yml"

// loadAppState loads recorded AppState from file
func loadAppState() (*AppState, error) {
appState := getDefaultAppState()

filepath, err := configFilePath("state.yml")
filepath, err := stateFilePath(stateFileName)
if err != nil {
if os.IsPermission(err) {
// apparently when people have read-only permissions they prefer us to fail silently
Expand Down

0 comments on commit d5ed386

Please sign in to comment.