| 1 |
package xdg |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"runtime" |
| 7 |
"strconv" |
| 8 |
) |
| 9 |
|
| 10 |
// Return a path relative to the user home cache dir |
| 11 |
func CachePath(paths ...string) string { |
| 12 |
res := filepath.Join(paths...) |
| 13 |
if !filepath.IsAbs(res) { |
| 14 |
var cache string |
| 15 |
if runtime.GOOS == "darwin" { |
| 16 |
// preserve backward compat with github.com/kyoh86/xdg |
| 17 |
cache = os.Getenv("XDG_CACHE_HOME") |
| 18 |
} |
| 19 |
if cache == "" { |
| 20 |
var err error |
| 21 |
cache, err = os.UserCacheDir() |
| 22 |
if err != nil { |
| 23 |
cache = ExpandHome("~/.cache") |
| 24 |
} |
| 25 |
} |
| 26 |
res = filepath.Join(cache, res) |
| 27 |
} |
| 28 |
return res |
| 29 |
} |
| 30 |
|
| 31 |
// Return a path relative to the user home config dir |
| 32 |
func ConfigPath(paths ...string) string { |
| 33 |
res := filepath.Join(paths...) |
| 34 |
if !filepath.IsAbs(res) { |
| 35 |
var config string |
| 36 |
if runtime.GOOS == "darwin" { |
| 37 |
// preserve backward compat with github.com/kyoh86/xdg |
| 38 |
config = os.Getenv("XDG_CONFIG_HOME") |
| 39 |
if config == "" { |
| 40 |
config = ExpandHome("~/Library/Preferences") |
| 41 |
} |
| 42 |
} else { |
| 43 |
var err error |
| 44 |
config, err = os.UserConfigDir() |
| 45 |
if err != nil { |
| 46 |
config = ExpandHome("~/.config") |
| 47 |
} |
| 48 |
} |
| 49 |
res = filepath.Join(config, res) |
| 50 |
} |
| 51 |
return res |
| 52 |
} |
| 53 |
|
| 54 |
// Return a path relative to the user data home dir |
| 55 |
func DataPath(paths ...string) string { |
| 56 |
res := filepath.Join(paths...) |
| 57 |
if !filepath.IsAbs(res) { |
| 58 |
data := os.Getenv("XDG_DATA_HOME") |
| 59 |
// preserve backward compat with github.com/kyoh86/xdg |
| 60 |
if data == "" && runtime.GOOS == "darwin" { |
| 61 |
data = ExpandHome("~/Library/Application Support") |
| 62 |
} else if data == "" { |
| 63 |
data = ExpandHome("~/.local/share") |
| 64 |
} |
| 65 |
res = filepath.Join(data, res) |
| 66 |
} |
| 67 |
return res |
| 68 |
} |
| 69 |
|
| 70 |
// Return a path relative to the user state home dir |
| 71 |
func StatePath(paths ...string) string { |
| 72 |
res := filepath.Join(paths...) |
| 73 |
if !filepath.IsAbs(res) { |
| 74 |
data := os.Getenv("XDG_STATE_HOME") |
| 75 |
if data == "" { |
| 76 |
data = ExpandHome("~/.local/state") |
| 77 |
} |
| 78 |
res = filepath.Join(data, res) |
| 79 |
} |
| 80 |
return res |
| 81 |
} |
| 82 |
|
| 83 |
// ugly: there's no other way to allow mocking a function in go... |
| 84 |
var userRuntimePath = func() string { |
| 85 |
uid := strconv.Itoa(os.Getuid()) |
| 86 |
path := filepath.Join("/run/user", uid) |
| 87 |
fi, err := os.Stat(path) |
| 88 |
if err != nil || !fi.Mode().IsDir() { |
| 89 |
// OpenRC does not create /run/user. TMUX and Neovim |
| 90 |
// create /tmp/$program-$uid instead. Mimic that. |
| 91 |
path = filepath.Join(os.TempDir(), "aerc-"+uid) |
| 92 |
err = os.MkdirAll(path, 0o700) |
| 93 |
if err != nil { |
| 94 |
// Fallback to /tmp if all else fails. |
| 95 |
path = os.TempDir() |
| 96 |
} |
| 97 |
} |
| 98 |
return path |
| 99 |
} |
| 100 |
|
| 101 |
// Return a path relative to the user runtime dir |
| 102 |
func RuntimePath(paths ...string) string { |
| 103 |
res := filepath.Join(paths...) |
| 104 |
if !filepath.IsAbs(res) { |
| 105 |
run := os.Getenv("XDG_RUNTIME_DIR") |
| 106 |
// preserve backward compat with github.com/kyoh86/xdg |
| 107 |
if run == "" && runtime.GOOS == "darwin" { |
| 108 |
run = ExpandHome("~/Library/Application Support") |
| 109 |
} else if run == "" { |
| 110 |
run = userRuntimePath() |
| 111 |
} |
| 112 |
res = filepath.Join(run, res) |
| 113 |
} |
| 114 |
return res |
| 115 |
} |