mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-25 12:24:47 +02:00
Use $XDG_STATE_DIR for state.yml
This commit is contained in:
parent
2118ecdf8e
commit
2c0520bc4b
@ -113,21 +113,9 @@ func isCustomConfigFile(path string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ConfigDir() string {
|
func ConfigDir() string {
|
||||||
legacyConfigDirectory := configDirForVendor("jesseduffield")
|
_, filePath := findConfigFile("config.yml")
|
||||||
if _, err := os.Stat(legacyConfigDirectory); !os.IsNotExist(err) {
|
|
||||||
return legacyConfigDirectory
|
|
||||||
}
|
|
||||||
configDirectory := configDirForVendor("")
|
|
||||||
return configDirectory
|
|
||||||
}
|
|
||||||
|
|
||||||
func configDirForVendor(vendor string) string {
|
return filepath.Dir(filePath)
|
||||||
envConfigDir := os.Getenv("CONFIG_DIR")
|
|
||||||
if envConfigDir != "" {
|
|
||||||
return envConfigDir
|
|
||||||
}
|
|
||||||
configDirs := xdg.New(vendor, "lazygit")
|
|
||||||
return configDirs.ConfigHome()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findOrCreateConfigDir() (string, error) {
|
func findOrCreateConfigDir() (string, error) {
|
||||||
@ -256,16 +244,50 @@ func (c *AppConfig) GetTempDir() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configFilePath(filename string) (string, error) {
|
func configFilePath(filename string) (string, error) {
|
||||||
folder, err := findOrCreateConfigDir()
|
exists, path := findConfigFile(filename)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
if exists {
|
||||||
|
return path, nil
|
||||||
|
}
|
||||||
|
return path, os.MkdirAll(filepath.Dir(path), 0o755)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(folder, filename), nil
|
// findConfigFile looks for a possibly existing config file.
|
||||||
|
// This function does NOT create any folders or files.
|
||||||
|
func findConfigFile(filename string) (exists bool, path string) {
|
||||||
|
if envConfigDir := os.Getenv("CONFIG_DIR"); envConfigDir != "" {
|
||||||
|
return true, filepath.Join(envConfigDir, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
// look for jesseduffield/lazygit/filename in XDG_CONFIG_HOME and XDG_CONFIG_DIRS
|
||||||
|
legacyConfigPath, err := xdg.SearchConfigFile(filepath.Join("jesseduffield", "lazygit", filename))
|
||||||
|
if err == nil {
|
||||||
|
return true, legacyConfigPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// look for lazygit/filename in XDG_CONFIG_HOME and XDG_CONFIG_DIRS
|
||||||
|
configFilepath, err := xdg.SearchConfigFile(filepath.Join("lazygit", filename))
|
||||||
|
if err == nil {
|
||||||
|
return true, configFilepath
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, filepath.Join(xdg.ConfigHome, "lazygit", filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ConfigFilename = "config.yml"
|
var ConfigFilename = "config.yml"
|
||||||
|
|
||||||
|
// stateFilePath looks for a possibly existing state file.
|
||||||
|
// if none exist, the default path is returned and all parent directories are created.
|
||||||
|
func stateFilePath(filename string) (string, error) {
|
||||||
|
exists, legacyStateFile := findConfigFile(filename)
|
||||||
|
if exists {
|
||||||
|
return legacyStateFile, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// looks for XDG_STATE_HOME/lazygit/filename
|
||||||
|
return xdg.StateFile(filepath.Join("lazygit", filename))
|
||||||
|
}
|
||||||
|
|
||||||
// ConfigFilename returns the filename of the default config file
|
// ConfigFilename returns the filename of the default config file
|
||||||
func (c *AppConfig) ConfigFilename() string {
|
func (c *AppConfig) ConfigFilename() string {
|
||||||
return filepath.Join(c.UserConfigDir, ConfigFilename)
|
return filepath.Join(c.UserConfigDir, ConfigFilename)
|
||||||
@ -278,7 +300,7 @@ func (c *AppConfig) SaveAppState() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath, err := configFilePath("state.yml")
|
filepath, err := stateFilePath(stateFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -292,11 +314,13 @@ func (c *AppConfig) SaveAppState() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var stateFileName = "state.yml"
|
||||||
|
|
||||||
// loadAppState loads recorded AppState from file
|
// loadAppState loads recorded AppState from file
|
||||||
func loadAppState() (*AppState, error) {
|
func loadAppState() (*AppState, error) {
|
||||||
appState := getDefaultAppState()
|
appState := getDefaultAppState()
|
||||||
|
|
||||||
filepath, err := configFilePath("state.yml")
|
filepath, err := stateFilePath(stateFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsPermission(err) {
|
if os.IsPermission(err) {
|
||||||
// apparently when people have read-only permissions they prefer us to fail silently
|
// apparently when people have read-only permissions they prefer us to fail silently
|
||||||
|
Loading…
x
Reference in New Issue
Block a user