-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_linux.go
193 lines (152 loc) · 3.72 KB
/
dir_linux.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package appdirs
import (
"os"
"os/user"
"path"
"strings"
)
const (
PATHSEP = ":"
)
func homeDir() (string, error) {
// first check for the $HOME var
if os.Getenv("HOME") != "" {
return os.Getenv("HOME"), nil
}
// otherwise retrieve from the user
usr, err := user.Current()
if err != nil {
return "", err
}
return usr.HomeDir, nil
}
func expandDir(dir string) (string, error) {
if !strings.HasPrefix(dir, "~/") {
return dir, nil
}
home, err := homeDir()
if err != nil {
return "", err
}
return path.Join(home, dir[2:]), nil
}
func (conf AppConf) userDataDir() (string, error) {
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
dataHome, err := expandDir(os.Getenv("XDG_DATA_HOME"))
if err != nil {
return "", err
}
if dataHome == "" {
home, err := HomeDir()
if err != nil {
return "", err
}
dataHome = path.Join(home, ".local/share")
}
// add app information
if conf.Name != "" {
dataHome = path.Join(dataHome, conf.Name)
if conf.Version != "" {
dataHome = path.Join(dataHome, conf.Version)
}
}
return dataHome, nil
}
func (conf AppConf) siteDataDir() (string, error) {
dataDir := "/usr/local/share"
if conf.Prefix != "" {
dataDir = path.Join(conf.Prefix, "share")
}
// special case if the prefix is /, use /usr/share
if conf.Prefix == "/" {
dataDir = "/usr/share"
}
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
if os.Getenv("XDG_DATA_DIRS") != "" {
var err error
dataDir, err = expandDir(strings.Split(os.Getenv("XDG_DATA_DIRS"), PATHSEP)[0])
if err != nil {
return "", nil
}
}
if conf.Name != "" {
dataDir = path.Join(dataDir, conf.Name)
if conf.Version != "" {
dataDir = path.Join(dataDir, conf.Version)
}
}
return dataDir, nil
}
func (conf AppConf) userConfigDir() (string, error) {
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
configHome, err := expandDir(os.Getenv("XDG_CONFIG_HOME"))
if err != nil {
return "", err
}
if configHome == "" {
home, err := HomeDir()
if err != nil {
return "", err
}
configHome = path.Join(home, ".config")
}
// add app information
if conf.Name != "" {
configHome = path.Join(configHome, conf.Name)
if conf.Version != "" {
configHome = path.Join(configHome, conf.Version)
}
}
return configHome, nil
}
func (conf AppConf) siteConfigDir() (string, error) {
configDir := "/etc/xdg"
if conf.Prefix != "" {
configDir = path.Join(conf.Prefix, "etc")
}
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
if os.Getenv("XDG_CONFIG_DIRS") != "" {
var err error
configDir, err = expandDir(strings.Split(os.Getenv("XDG_CONFIG_DIRS"), PATHSEP)[0])
if err != nil {
return "", nil
}
}
if conf.Name != "" {
configDir = path.Join(configDir, conf.Name)
if conf.Version != "" {
configDir = path.Join(configDir, conf.Version)
}
}
return configDir, nil
}
func (conf AppConf) userCacheDir() (string, error) {
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
cacheDir, err := expandDir(os.Getenv("XDG_CACHE_HOME"))
if err != nil {
return "", err
}
if cacheDir == "" {
home, err := HomeDir()
if err != nil {
return "", err
}
cacheDir = path.Join(home, ".cache")
}
// add app information
if conf.Name != "" {
cacheDir = path.Join(cacheDir, conf.Name)
if conf.Version != "" {
cacheDir = path.Join(cacheDir, conf.Version)
}
}
return cacheDir, nil
}
func (conf AppConf) userLogDir() (string, error) {
// first use the XDG_DATA_HOME env variable, otherwise fallback to a safe default
cacheDir, err := conf.userCacheDir()
if err != nil {
return "", err
}
return path.Join(cacheDir, "logs"), nil
}