-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (41 loc) · 793 Bytes
/
main.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
package main
import (
"io/ioutil"
"os"
"os/exec"
"path"
)
func main() {
cwd, _ := os.Getwd()
dir := cwd
gopath := os.Getenv("GOPATH")
for {
if _, err := os.Stat(path.Join(dir, ".gopath")); err == nil {
gopath, err = String(ioutil.ReadFile(path.Join(dir, ".gopath")))
if err != nil || gopath == "" {
gopath = dir
}
if !path.IsAbs(gopath) {
gopath = path.Join(dir, gopath)
}
break
}
if dir == "/" {
break
}
dir = path.Dir(dir)
}
os.Setenv("GOPATH", gopath)
if len(os.Args) < 2 {
os.Stdout.WriteString(gopath + "\n")
os.Exit(0)
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func String(b []byte, err error) (string, error) {
return string(b), err
}