Support default value of $GOPATH (#532)

Add support for go1.8 of blank $GOPATH
This commit is contained in:
Elliott Beach 2017-09-29 11:16:12 -05:00 committed by Albert Nigmatzianov
parent b787445794
commit e5f66de850
1 changed files with 22 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"text/template" "text/template"
@ -31,7 +32,27 @@ func init() {
envGoPath := os.Getenv("GOPATH") envGoPath := os.Getenv("GOPATH")
goPaths := filepath.SplitList(envGoPath) goPaths := filepath.SplitList(envGoPath)
if len(goPaths) == 0 { if len(goPaths) == 0 {
er("$GOPATH is not set") // Adapted from https://github.com/Masterminds/glide/pull/798/files.
// As of Go 1.8 the GOPATH is no longer required to be set. Instead there
// is a default value. If there is no GOPATH check for the default value.
// Note, checking the GOPATH first to avoid invoking the go toolchain if
// possible.
goExecutable := os.Getenv("COBRA_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOPATH").Output()
if err != nil {
er(err)
}
toolchainGoPath := strings.TrimSpace(string(out))
goPaths = filepath.SplitList(toolchainGoPath)
if len(goPaths) == 0 {
er("$GOPATH is not set")
}
} }
srcPaths = make([]string, 0, len(goPaths)) srcPaths = make([]string, 0, len(goPaths))
for _, goPath := range goPaths { for _, goPath := range goPaths {