Skip to content

Commit

Permalink
Merge pull request #991 from rsteube/xdg-configdirs
Browse files Browse the repository at this point in the history
xdg: global config dirs
  • Loading branch information
rsteube authored Feb 14, 2024
2 parents d3f0050 + 46596aa commit 38c4ae6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pkg/xdg/xdg.go
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -14,11 +17,38 @@ 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()
}
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
}

0 comments on commit 38c4ae6

Please sign in to comment.