diff --git a/pkg/xdg/xdg.go b/pkg/xdg/xdg.go index 7465ec3b2..3ffd79bb4 100644 --- a/pkg/xdg/xdg.go +++ b/pkg/xdg/xdg.go @@ -1,11 +1,14 @@ package xdg import ( + "errors" "os" "path/filepath" + "runtime" + "strings" ) -// UserCacheDir returns the cache base directory. +// UserCacheDir returns the user cache base directory. func UserCacheDir() (dir string, err error) { if dir = os.Getenv("XDG_CACHE_HOME"); dir == "" { dir, err = os.UserCacheDir() @@ -14,7 +17,7 @@ func UserCacheDir() (dir string, err error) { return } -// UserConfigDir returns the config base directory. +// UserConfigDir returns the user config base directory. func UserConfigDir() (dir string, err error) { if dir = os.Getenv("XDG_CONFIG_HOME"); dir == "" { dir, err = os.UserConfigDir() @@ -22,3 +25,30 @@ func UserConfigDir() (dir string, err error) { dir = filepath.ToSlash(dir) return } + +// ConfigDirs returns the global config base directories. +func ConfigDirs() (dirs []string, err error) { + switch runtime.GOOS { + case "windows": + dir, ok := os.LookupEnv("PROGRAMDATA") + if !ok { + return nil, errors.New("missing PROGRAMDATA environment variable") + } + dirs = append(dirs, dir) + + case "darwin": + dirs = append(dirs, "/Library/Application Support") + default: + dirs = append(dirs, "/etc/xdg") + + } + + if v, ok := os.LookupEnv("XDG_CONFIG_DIRS"); ok { + dirs = append(strings.Split(v, string(os.PathSeparator)), dirs...) + } + + for index, dir := range dirs { + dirs[index] = filepath.ToSlash(dir) + } + return +}