nasin/path.go

59 lines
2.1 KiB
Go

package nasin
import "os"
import "path/filepath"
// 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)
}
// ApplicationSystemDataDirs returns a list of directory paths where an
// 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.
func ApplicationSystemDataDirs (app ApplicationDescription) ([]string, error) {
return systemDirs(app.ID, systemDataDirs)
}
// 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)
}
// ApplicationSystemDataDirs returns a list of directory paths where an
// 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.
func ApplicationSystemConfigDirs (app ApplicationDescription) ([]string, error) {
return systemDirs(app.ID, systemConfigDirs)
}
// 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)
}
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
}
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
}