2024-04-28 22:53:03 -06:00
|
|
|
package nasin
|
2024-04-28 22:47:50 -06:00
|
|
|
|
|
|
|
import "os"
|
|
|
|
import "path/filepath"
|
|
|
|
|
2024-06-06 20:55:14 -06:00
|
|
|
// ApplicationUserDataDir returns the directory path where an application can
|
|
|
|
// store its user data files. If the directory does not exist, it will be
|
|
|
|
// created.
|
|
|
|
func ApplicationUserDataDir (app ApplicationDescription) (string, error) {
|
|
|
|
return userMkdirAll(app.ID, userDataDir)
|
2024-04-28 22:47:50 -06:00
|
|
|
}
|
|
|
|
|
2024-08-22 11:37:38 -06:00
|
|
|
// ApplicationSystemDataDirs returns a list of directory paths where an
|
2024-08-23 00:27:49 -06:00
|
|
|
// application can look for its system-level data files. Directories returned
|
|
|
|
// by this function may or may not actually exist. This function may return an
|
|
|
|
// empty slice on some platforms.
|
2024-08-22 11:37:38 -06:00
|
|
|
func ApplicationSystemDataDirs (app ApplicationDescription) ([]string, error) {
|
|
|
|
return systemDirs(app.ID, systemDataDirs)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:55:14 -06:00
|
|
|
// ApplicationUserConfigDir returns the directory path where an application can
|
|
|
|
// store its user configuration files.
|
|
|
|
func ApplicationUserConfigDir (app ApplicationDescription) (string, error) {
|
|
|
|
return userMkdirAll(app.ID, userConfigDir)
|
2024-04-28 22:47:50 -06:00
|
|
|
}
|
|
|
|
|
2024-08-22 11:37:38 -06:00
|
|
|
// ApplicationSystemDataDirs returns a list of directory paths where an
|
2024-08-23 00:27:49 -06:00
|
|
|
// application can look for its system-level configuration files. Directories
|
|
|
|
// returned by this function may or may not actually exist. This function may
|
|
|
|
// return an empty slice on some platforms.
|
2024-08-22 11:37:38 -06:00
|
|
|
func ApplicationSystemConfigDirs (app ApplicationDescription) ([]string, error) {
|
|
|
|
return systemDirs(app.ID, systemConfigDirs)
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:55:14 -06:00
|
|
|
// ApplicationUserCacheDir returns the directory path where an application can
|
|
|
|
// store its user cache files.
|
|
|
|
func ApplicationUserCacheDir (app ApplicationDescription) (string, error) {
|
|
|
|
return userMkdirAll(app.ID, userCacheDir)
|
2024-04-28 22:47:50 -06:00
|
|
|
}
|
|
|
|
|
2024-06-06 20:55:14 -06:00
|
|
|
func userMkdirAll (sub string, getter func () (string, error)) (string, error) {
|
|
|
|
path, err := getter()
|
|
|
|
if err != nil { return "", err }
|
|
|
|
path = filepath.Join(path, sub)
|
|
|
|
err = os.MkdirAll(path, 0700)
|
|
|
|
if err != nil { return "", err }
|
|
|
|
return path, nil
|
2024-04-28 22:47:50 -06:00
|
|
|
}
|
2024-08-22 11:37:38 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|