-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
130 lines (105 loc) · 2.65 KB
/
env.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
package golangx
import (
"go/build"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/aesoper101/x/filex"
"github.com/aesoper101/x/str"
)
var defaultGoProxy = "https://goproxy.cn,direct"
// GoBin returns the path to the go binary.
func GoBin() string {
bin := os.Getenv("GOBIN")
if bin != "" {
if filex.IsExists(bin) {
return bin
}
}
gopath := GoPath()
bin = filepath.Join(gopath, "bin")
if filex.IsExists(bin) {
return bin
}
goroot := GoRoot()
bin = filepath.Join(goroot, "bin")
if filex.IsExists(bin) {
return bin
}
ctx := build.Default
return filepath.Join(ctx.GOPATH, "bin")
}
// GoProxy returns the proxy setting for go. if not set,
// return a default proxy setting: "https://goproxy.cn,direct".
func GoProxy() string {
proxy := os.Getenv("GOPROXY")
if proxy != "" {
return proxy
}
if output, _ := exec.Command("go", "env", "GOPROXY").Output(); str.IsNotEmpty(string(output)) {
return string(output)
}
return defaultGoProxy
}
// IsGO111ModuleOn returns true if go111module is set to on.
func IsGO111ModuleOn() bool {
v := os.Getenv("GO111MODULE")
if str.IsNotEmpty(v) {
return strings.ToLower(v) == "on"
}
if output, _ := exec.Command("go", "env", "GO111MODULE").Output(); str.IsNotEmpty(string(output)) {
return strings.ToLower(string(output)) == "on"
}
return true
}
// GoPath returns the GOPATH.
func GoPath() string {
path := os.Getenv("GOPATH")
if str.IsNotEmpty(path) {
return splitGoPath(path)[0]
}
if output, _ := exec.Command("go", "env", "GOPATH").Output(); str.IsNotEmpty(string(output)) {
return string(output)
}
return build.Default.GOPATH
}
// GoRoot returns the GOROOT.
func GoRoot() string {
path := os.Getenv("GOROOT")
if str.IsNotEmpty(path) {
return path
}
if output, _ := exec.Command("go", "env", "GOROOT").Output(); str.IsNotEmpty(string(output)) {
return string(output)
}
return build.Default.GOROOT
}
// GoVersion returns the go version.
func GoVersion() string {
if output, _ := exec.Command("go", "env", "GOVERSION").Output(); str.IsNotEmpty(string(output)) {
return strings.ReplaceAll(strings.Replace(string(output), "\n", "", -1), "go", "")
}
return ""
}
// GoModCache returns the path of Go module cache.
func GoModCache() string {
cacheOut, _ := exec.Command("go", "env", "GOMODCACHE").Output()
cachePath := strings.Trim(string(cacheOut), "\n")
if str.IsEmpty(cachePath) {
return filepath.Join(GoPath(), "pkg", "mod")
}
return cachePath
}
func splitGoPath(goPath string) []string {
var sep string
goos := strings.ToLower(runtime.GOOS)
switch goos {
case "windows":
sep = ";"
case "linux", "darwin":
sep = ":"
}
return strings.Split(goPath, sep)
}