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