Skip to content

Commit

Permalink
Merge pull request #717 from heyitsanthony/cache-goroot
Browse files Browse the repository at this point in the history
Cache GOROOT at init time
mattfarina committed Apr 7, 2017
2 parents 869001d + 6bf3fc6 commit cf18c82
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -22,11 +22,25 @@ import (
// other needs arise it may need to be re-written.
var ResolveCurrent = false

// goRoot caches the GOROOT variable for build contexts. If $GOROOT is not set in
// the user's environment, then the context's root path is 'go env GOROOT'.
var goRoot string

func init() {
// Precompile the regular expressions used to check VCS locations.
for _, v := range vcsList {
v.regex = regexp.MustCompile(v.pattern)
}
if goRoot = os.Getenv("GOROOT"); len(goRoot) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOROOT").Output()
if err == nil {
goRoot = strings.TrimSpace(string(out))
}
}
}

func toSlash(v string) string {
@@ -272,6 +286,11 @@ func (b *BuildCtxt) PackageName(base string) string {
//
// TODO: This should be moved to the `dependency` package.
func GetBuildContext() (*BuildCtxt, error) {
if len(goRoot) == 0 {
return nil, fmt.Errorf("GOROOT value not found. Please set the GOROOT " +
"environment variable to use this command")
}

buildContext := &BuildCtxt{build.Default}

// If we aren't resolving for the current system set to look at all
@@ -282,18 +301,7 @@ func GetBuildContext() (*BuildCtxt, error) {
buildContext.UseAllFiles = true
}

if goRoot := os.Getenv("GOROOT"); len(goRoot) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOROOT").Output()
if goRoot = strings.TrimSpace(string(out)); len(goRoot) == 0 || err != nil {
return nil, fmt.Errorf("Please set the $GOROOT environment " +
"variable to use this command\n")
}
buildContext.GOROOT = goRoot
}
buildContext.GOROOT = goRoot
return buildContext, nil
}

0 comments on commit cf18c82

Please sign in to comment.