-
Notifications
You must be signed in to change notification settings - Fork 2
/
go.go
74 lines (63 loc) · 2.02 KB
/
go.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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/essentials source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ess
import (
"errors"
"go/build"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Required variables
var (
ErrGoPathIsNotSet = errors.New("GOPATH environment variable is not set. " +
"Please refer to https://golang.org/doc/code.html to configure your Go environment")
ErrDirNotInGoPath = errors.New("current directory is outside of GOPATH")
)
// LookExecutable looks for an executable binary named file
// in the directories named by the PATH environment variable.
func LookExecutable(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}
// IsImportPathExists returns true if import path found in the GOPATH
// otherwise returns false
func IsImportPathExists(path string) bool {
_, err := build.Import(path, "", build.FindOnly)
return err == nil
}
// GoPath returns GOPATH in context with current working directory otherwise
// it returns first directory from GOPATH
func GoPath() (string, error) {
gopath := build.Default.GOPATH
if IsStrEmpty(gopath) {
return "", ErrGoPathIsNotSet
}
var currentGoPath string
workingDir, _ := os.Getwd()
goPathList := filepath.SplitList(gopath)
for _, path := range goPathList {
if strings.HasPrefix(strings.ToLower(workingDir), strings.ToLower(path)) {
currentGoPath = path
break
}
path, _ = filepath.EvalSymlinks(path)
if len(path) > 0 && strings.HasPrefix(strings.ToLower(workingDir), strings.ToLower(path)) {
currentGoPath = path
break
}
}
if IsStrEmpty(currentGoPath) {
// current working dir didn't match up,
// so pick first one
currentGoPath = goPathList[0]
}
return currentGoPath, nil
}
// IsInGoRoot returns true if given path has prefix of GOROOT otherwise false
func IsInGoRoot(path string) bool {
return strings.HasPrefix(path, filepath.Join(build.Default.GOROOT, "src")) ||
strings.HasPrefix(path, filepath.Join(build.Default.GOROOT, "pkg"))
}