-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
base_dirs.go
82 lines (66 loc) · 2.2 KB
/
base_dirs.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
package xdg
import (
"os"
"github.com/adrg/xdg/internal/pathutil"
)
// XDG Base Directory environment variables.
const (
envDataHome = "XDG_DATA_HOME"
envDataDirs = "XDG_DATA_DIRS"
envConfigHome = "XDG_CONFIG_HOME"
envConfigDirs = "XDG_CONFIG_DIRS"
envStateHome = "XDG_STATE_HOME"
envCacheHome = "XDG_CACHE_HOME"
envRuntimeDir = "XDG_RUNTIME_DIR"
// Non-standard.
envBinHome = "XDG_BIN_HOME"
)
type baseDirectories struct {
dataHome string
data []string
configHome string
config []string
stateHome string
cacheHome string
runtime string
// Non-standard.
binHome string
fonts []string
applications []string
}
func (bd baseDirectories) dataFile(relPath string) (string, error) {
return pathutil.Create(relPath, append([]string{bd.dataHome}, bd.data...))
}
func (bd baseDirectories) configFile(relPath string) (string, error) {
return pathutil.Create(relPath, append([]string{bd.configHome}, bd.config...))
}
func (bd baseDirectories) stateFile(relPath string) (string, error) {
return pathutil.Create(relPath, []string{bd.stateHome})
}
func (bd baseDirectories) cacheFile(relPath string) (string, error) {
return pathutil.Create(relPath, []string{bd.cacheHome})
}
func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
var paths []string
for _, p := range pathutil.Unique([]string{bd.runtime, os.TempDir()}) {
if pathutil.Exists(p) {
paths = append(paths, p)
}
}
return pathutil.Create(relPath, paths)
}
func (bd baseDirectories) searchDataFile(relPath string) (string, error) {
return pathutil.Search(relPath, append([]string{bd.dataHome}, bd.data...))
}
func (bd baseDirectories) searchConfigFile(relPath string) (string, error) {
return pathutil.Search(relPath, append([]string{bd.configHome}, bd.config...))
}
func (bd baseDirectories) searchStateFile(relPath string) (string, error) {
return pathutil.Search(relPath, []string{bd.stateHome})
}
func (bd baseDirectories) searchCacheFile(relPath string) (string, error) {
return pathutil.Search(relPath, []string{bd.cacheHome})
}
func (bd baseDirectories) searchRuntimeFile(relPath string) (string, error) {
return pathutil.Search(relPath, pathutil.Unique([]string{bd.runtime, os.TempDir()}))
}