forked from therecipe/qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopath.go
52 lines (46 loc) · 1.01 KB
/
gopath.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
package utils
import (
"go/build"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
const PackageName = "github.com/akiyosi/qt"
var (
mustGoPath string
mustGoPathMutex = new(sync.Mutex)
)
func GOBIN() string {
if QT_DOCKER() {
for _, p := range filepath.SplitList(GOPATH()) {
if strings.HasPrefix(p, os.Getenv("HOME")) {
return filepath.Join(p, "bin")
}
}
}
return envOr("GOBIN", filepath.Join(MustGoPath(), "bin"))
}
func GOPATH() string { return envOr("GOPATH", build.Default.GOPATH) }
// MustGoPath returns the GOPATH that holds this package
func MustGoPath() string {
mustGoPathMutex.Lock()
if mustGoPath == "" {
if _, err := exec.LookPath("go"); err == nil && !UseGOMOD("") {
mustGoPath = strings.TrimSpace(RunCmd(GoList("{{.Root}}", PackageName, "-find"), "get list gopath"))
}
if mustGoPath == "" {
mustGoPath = GOPATH()
}
}
mustGoPathMutex.Unlock()
return mustGoPath
}
func envOr(name, def string) string {
s := os.Getenv(name)
if s == "" {
return def
}
return s
}