forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopath.go
75 lines (72 loc) · 1.97 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package goutil
import (
"errors"
"go/build"
"os"
"path/filepath"
"strings"
)
// GetGopaths returns the list of Go path directories.
func GetGopaths() []string {
var all []string
for _, p := range filepath.SplitList(build.Default.GOPATH) {
if p == "" || p == build.Default.GOROOT {
// Empty paths are uninteresting.
// If the path is the GOROOT, ignore it.
// People sometimes set GOPATH=$GOROOT.
// Do not get confused by this common mistake.
continue
}
if strings.HasPrefix(p, "~") {
// Path segments starting with ~ on Unix are almost always
// users who have incorrectly quoted ~ while setting GOPATH,
// preventing it from expanding to $HOME.
// The situation is made more confusing by the fact that
// bash allows quoted ~ in $PATH (most shells do not).
// Do not get confused by this, and do not try to use the path.
// It does not exist, and printing errors about it confuses
// those users even more, because they think "sure ~ exists!".
// The go command diagnoses this situation and prints a
// useful error.
// On Windows, ~ is used in short names, such as c:\progra~1
// for c:\program files.
continue
}
all = append(all, p)
}
for k, v := range all {
// GOPATH should end with / or \
if strings.HasSuffix(v, "/") || strings.HasSuffix(v, string(os.PathSeparator)) {
continue
}
v += string(os.PathSeparator)
all[k] = v
}
return all
}
// GetFirstGopath gets the first $GOPATH value.
func GetFirstGopath(allowAutomaticGuessing bool) (gopath string, err error) {
a := GetGopaths()
if len(a) > 0 {
gopath = a[0]
}
defer func() {
gopath = strings.Replace(gopath, "/", string(os.PathSeparator), -1)
}()
if gopath != "" {
return
}
if !allowAutomaticGuessing {
err = errors.New("not found GOPATH")
return
}
p, _ := os.Getwd()
p = strings.Replace(p, "\\", "/", -1) + "/"
i := strings.LastIndex(p, "/src/")
if i == -1 {
err = errors.New("not found GOPATH")
return
}
gopath = p[:i+1]
return
}