forked from metaleap/go-xsd
/
sys.go
86 lines (78 loc) · 2.33 KB
/
sys.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
package usys
import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/UNO-SOFT/go-xsd/util/fs"
)
var (
_userHomeDirPath string
_userDataDirPaths = make(map[bool]string, 2)
// Look-up hash-table for the `OSName` function.
OSNames = map[string]string{
"windows": "Windows",
"darwin": "Mac OS X",
"linux": "Linux",
"freebsd": "FreeBSD",
"appengine": "Google App Engine",
}
)
// Short-hand for: `runtime.GOMAXPROCS(2 * runtime.NumCPU())`.
func MaxProcs() {
runtime.GOMAXPROCS(2 * runtime.NumCPU())
}
// Returns the human-readable operating system name represented by the specified
// `goOS` name, by looking up the corresponding entry in `OSNames`.
func OSName(goOS string) (name string) {
if name = OSNames[goOS]; len(name) == 0 {
name = strings.ToTitle(goOS)
}
return
}
func UserDataDirPath(preferCacheOverConfig bool) string {
dirpath := _userDataDirPaths[preferCacheOverConfig]
if len(dirpath) == 0 {
probeenvvars := []string{"XDG_CONFIG_HOME", "XDG_CACHE_HOME", "LOCALAPPDATA", "APPDATA"}
if preferCacheOverConfig {
probeenvvars[0], probeenvvars[1] = probeenvvars[1], probeenvvars[0]
}
for _, envvar := range probeenvvars {
if maybedirpath := os.Getenv(envvar); len(maybedirpath) > 0 && ufs.DirExists(maybedirpath) {
dirpath = maybedirpath
break
}
}
if len(dirpath) == 0 {
probehomesubdirs := []string{".config", ".cache", "Library/Caches", "Library/Application Support"}
if preferCacheOverConfig {
probehomesubdirs[0], probehomesubdirs[1] = probehomesubdirs[1], probehomesubdirs[0]
}
for _, homesubdir := range probehomesubdirs {
if maybedirpath := filepath.Join(UserHomeDirPath(), homesubdir); ufs.DirExists(maybedirpath) {
dirpath = maybedirpath
break
}
}
if len(dirpath) == 0 {
dirpath = UserHomeDirPath()
}
}
_userDataDirPaths[preferCacheOverConfig] = dirpath
}
return dirpath
}
// Returns the path to the current user's home directory.
func UserHomeDirPath() string {
dirpath := _userHomeDirPath
if len(dirpath) == 0 {
if user, err := user.Current(); err == nil && len(user.HomeDir) > 0 && ufs.DirExists(user.HomeDir) {
dirpath = user.HomeDir
} else if dirpath = os.Getenv("USERPROFILE"); len(dirpath) == 0 || !ufs.DirExists(dirpath) {
dirpath = os.Getenv("HOME")
}
_userHomeDirPath = dirpath
}
return dirpath
}