diff options
author | Ollie Phillips <oliver@eantics.co.uk> | 2017-03-01 10:27:04 +0000 |
---|---|---|
committer | Ollie Phillips <oliver@eantics.co.uk> | 2017-03-01 10:27:04 +0000 |
commit | b0975d15ad64630b0e5830a5f28e07a2d3b6908d (patch) | |
tree | 86b4a0b9d537357b601a21a0856f20644f808c4a /cmd/ponzu/add.go | |
parent | 88dc9b4d26022aeda295e03690c4eb4398e40487 (diff) |
revised GOPATH detection for bug and Go 1.8 default
Diffstat (limited to 'cmd/ponzu/add.go')
-rw-r--r-- | cmd/ponzu/add.go | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/cmd/ponzu/add.go b/cmd/ponzu/add.go index dd8af6c..abac59c 100644 --- a/cmd/ponzu/add.go +++ b/cmd/ponzu/add.go @@ -6,7 +6,9 @@ import ( "io" "os" "os/exec" + "os/user" "path/filepath" + "runtime" "strings" ) @@ -34,9 +36,11 @@ func getAddon(args []string) error { } // copy to ./addons folder - // GOPATH can be a list delimited by ":" on Linux or ";" on Windows - // `go get` uses the first, this should parse out the first whatever the OS - gopath := resolveGOPATH() + // resolve GOPATH + gopath, err := getGOPATH() + if err != nil { + return addError(err) + } pwd, err := os.Getwd() if err != nil { @@ -56,16 +60,29 @@ func getAddon(args []string) error { return nil } -// GOPATH can be a list delimited by ":" on Linux or ";" on Windows -// `go get` uses saves packages to the first entry, so this function -// should parse out the first whatever the OS -func resolveGOPATH() string { - envGOPATH := os.Getenv("GOPATH") - gopaths := strings.Split(envGOPATH, ":") - gopath := gopaths[0] - gopaths = strings.Split(envGOPATH, ";") - gopath = gopaths[0] - return gopath +// resolve GOPATH. In 1.8 can be default, or custom. A custom GOPATH can +// also contain multiple paths, in which case 'go get' uses the first +func getGOPATH() (string, error) { + var gopath string + gopath = os.Getenv("GOPATH") + if gopath == "" { + // not set, find the default + usr, err := user.Current() + if err != nil { + return gopath, err + } + gopath = filepath.Join(usr.HomeDir, "go") + } else { + // parse out in case of multiple, retain first + if runtime.GOOS == "windows" { + gopaths := strings.Split(gopath, ";") + gopath = gopaths[0] + } else { + gopaths := strings.Split(gopath, ":") + gopath = gopaths[0] + } + } + return gopath, nil } // this is distinct from copyAll() in that files are copied, not moved, |