forked from casimir/xdg-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxdg.go
More file actions
93 lines (77 loc) · 2.39 KB
/
xdg.go
File metadata and controls
93 lines (77 loc) · 2.39 KB
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
package xdg
import (
"os"
"path/filepath"
"strings"
)
// App is an aggregation of XDG information for a named application. Useful to
// propagate app configuration.
type App struct {
Name string
}
func (a App) path(envVar string, defaultFn func() string, file string) string {
base := os.Getenv(envVar)
if base == "" {
base = defaultFn()
}
return filepath.Join(base, a.Name, file)
}
// DataPath determines the full path of a data file.
func (a App) DataPath(file string) string {
return a.path("XDG_DATA_HOME", DataHome, file)
}
// ConfigPath determines the full path of a data file.
func (a App) ConfigPath(file string) string {
return a.path("XDG_CONFIG_HOME", ConfigHome, file)
}
// CachePath determines the full path of a cached file.
func (a App) CachePath(file string) string {
return a.path("XDG_CACHE_HOME", CacheHome, file)
}
func (a App) multiPaths(envVar string, defaultFn func() []string, file string) []string {
var bases []string
env := os.Getenv(envVar)
if env != "" {
bases = strings.Split(env, ":")
} else {
bases = defaultFn()
}
var dirs []string
for _, it := range bases {
dirs = append(dirs, filepath.Join(it, a.Name, file))
}
return dirs
}
// SystemDataPaths determines system-wide possible paths for a data file.
func (a App) SystemDataPaths(file string) []string {
return a.multiPaths("XDG_DATA_DIRS", DataDirs, file)
}
// SystemConfigPaths determines system-wide possible paths for a config file.
func (a App) SystemConfigPaths(file string) []string {
return a.multiPaths("XDG_CONFIG_DIRS", ConfigDirs, file)
}
var defaultApp App
// SetName for the default application. Used for package-wide functions.
func SetName(name string) {
defaultApp.Name = name
}
// DataPath determines the full path of a data file.
func DataPath(file string) string {
return defaultApp.DataPath(file)
}
// ConfigPath determines the full path of a data file.
func ConfigPath(file string) string {
return defaultApp.ConfigPath(file)
}
// CachePath determines the full path of a cached file.
func CachePath(file string) string {
return defaultApp.CachePath(file)
}
// SystemDataPaths determines system-wide possible paths for a data file.
func SystemDataPaths(file string) []string {
return defaultApp.SystemDataPaths(file)
}
// SystemConfigPaths determines system-wide possible paths for a config file.
func SystemConfigPaths(file string) []string {
return defaultApp.SystemConfigPaths(file)
}