diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index aed26be043a9..d51dedaf51f8 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -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) { @@ -245,16 +233,50 @@ func (c *AppConfig) GetTempDir() string { } func configFilePath(filename string) (string, error) { - folder, err := findOrCreateConfigDir() - if err != nil { - return "", err + exists, path := findConfigFile(filename) + + if exists { + return path, nil + } else { + return path, os.MkdirAll(filepath.Dir(path), 0o755) + } +} + +func findConfigFile(filename string) (exists 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 + } + + exists, legacyStateFile := findConfigFile(filename) + + if exists { + 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) @@ -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 } @@ -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