Add system dirs to path.go

This commit is contained in:
Sasha Koshka 2024-08-22 13:37:38 -04:00
parent 92deac2d56
commit d2672816cd
2 changed files with 33 additions and 0 deletions

25
path.go
View File

@ -10,12 +10,27 @@ func ApplicationUserDataDir (app ApplicationDescription) (string, error) {
return userMkdirAll(app.ID, userDataDir) return userMkdirAll(app.ID, userDataDir)
} }
// ApplicationSystemDataDirs returns a list of directory paths where an
// application can look for its system-level data files. These directories may
// or may not exist. This function may return an empty slice on some platforms.
func ApplicationSystemDataDirs (app ApplicationDescription) ([]string, error) {
return systemDirs(app.ID, systemDataDirs)
}
// ApplicationUserConfigDir returns the directory path where an application can // ApplicationUserConfigDir returns the directory path where an application can
// store its user configuration files. // store its user configuration files.
func ApplicationUserConfigDir (app ApplicationDescription) (string, error) { func ApplicationUserConfigDir (app ApplicationDescription) (string, error) {
return userMkdirAll(app.ID, userConfigDir) return userMkdirAll(app.ID, userConfigDir)
} }
// ApplicationSystemDataDirs returns a list of directory paths where an
// application can look for its system-level configuration files. These
// directories may or may not exist. This function may return an empty slice on
// some platforms.
func ApplicationSystemConfigDirs (app ApplicationDescription) ([]string, error) {
return systemDirs(app.ID, systemConfigDirs)
}
// ApplicationUserCacheDir returns the directory path where an application can // ApplicationUserCacheDir returns the directory path where an application can
// store its user cache files. // store its user cache files.
func ApplicationUserCacheDir (app ApplicationDescription) (string, error) { func ApplicationUserCacheDir (app ApplicationDescription) (string, error) {
@ -30,3 +45,13 @@ func userMkdirAll (sub string, getter func () (string, error)) (string, error) {
if err != nil { return "", err } if err != nil { return "", err }
return path, nil return path, nil
} }
func systemDirs (sub string, getter func () ([]string, error)) ([]string, error) {
paths, err := getter()
if err != nil { return nil, err }
specificPaths := make([]string, len(paths))
for index, path := range paths {
specificPaths[index] = filepath.Join(path, sub)
}
return specificPaths, nil
}

View File

@ -7,10 +7,18 @@ func userDataDir () (string, error) {
return basedir.DataHome() return basedir.DataHome()
} }
func systemDataDirs () ([]string, error) {
return basedir.DataDirs()
}
func userConfigDir () (string, error) { func userConfigDir () (string, error) {
return basedir.ConfigHome() return basedir.ConfigHome()
} }
func systemConfigDirs () ([]string, error) {
return basedir.ConfigDirs()
}
func userCacheDir () (string, error) { func userCacheDir () (string, error) {
return basedir.CacheHome() return basedir.CacheHome()
} }