-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirs.go
87 lines (81 loc) · 1.92 KB
/
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
83
84
85
86
87
package dirs
import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
)
// GetEnv is a wrapper of [os.Getenv] implementing fallback for some environment variables.
func GetEnv(key string) string {
switch key {
case "HOME":
return HomeDir("")
}
ret := os.Getenv(key)
if strings.HasPrefix(key, "XDG_") {
if ret != "" {
return ret
}
switch key {
case "XDG_CONFIG_HOME":
return filepath.Join(HomeDir(""), ".config")
case "XDG_CACHE_HOME":
return filepath.Join(HomeDir(""), ".cache")
case "XDG_DATA_HOME":
return filepath.Join(HomeDir(""), ".local", "share")
case "XDG_STATE_HOME":
return filepath.Join(HomeDir(""), ".local", "state")
case "XDG_DATA_DIRS":
return filepath.Join(RootDir(), "usr", "local", "share") +
":" + filepath.Join(RootDir(), "usr", "share")
case "XDG_CONFIG_DIRS":
return filepath.Join(RootDir(), "etc", "xdg")
}
}
return ret
}
// ExpandEnv works like [os.ExpandEnv] but uses [GetEnv].
func ExpandEnv(s string) string {
return os.Expand(s, GetEnv)
}
// RootDir returns the system root directory.
func RootDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("SYSTEMDRIVE") + string(os.PathSeparator)
}
return string(os.PathSeparator)
}
// HomeDir returns the user's home directory.
func HomeDir(usr string) string {
var u *user.User
var err error
if usr == "" {
if h, err := os.UserHomeDir(); err == nil {
return h
}
u, err = user.Current()
} else {
u, err = user.Lookup(usr)
}
if err == nil && u != nil {
return u.HomeDir
}
return ""
}
// ExpandTilde expands tilde "~" into the home directory.
func ExpandTilde(s string) string {
const pathSeparator = string(os.PathSeparator)
s = filepath.FromSlash(s)
if strings.HasPrefix(s, "~") {
u, p, sep := strings.Cut(s[1:], pathSeparator)
if sep {
// ~/path or ~user/path
return HomeDir(u) + pathSeparator + p
}
// ~ or ~user
return HomeDir(u)
}
// path or /path
return s
}