From d2672816cdf64daf86f80ecb7dd137c0994bcace Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 22 Aug 2024 13:37:38 -0400 Subject: [PATCH] Add system dirs to path.go --- path.go | 25 +++++++++++++++++++++++++ path_unix.go | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/path.go b/path.go index 331ce00..0439aa4 100644 --- a/path.go +++ b/path.go @@ -10,12 +10,27 @@ 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. 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 // 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. 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 // store its user cache files. 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 } 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 +} diff --git a/path_unix.go b/path_unix.go index a93be6a..efdacf7 100644 --- a/path_unix.go +++ b/path_unix.go @@ -7,10 +7,18 @@ func userDataDir () (string, error) { return basedir.DataHome() } +func systemDataDirs () ([]string, error) { + return basedir.DataDirs() +} + func userConfigDir () (string, error) { return basedir.ConfigHome() } +func systemConfigDirs () ([]string, error) { + return basedir.ConfigDirs() +} + func userCacheDir () (string, error) { return basedir.CacheHome() }