github.com/jd-ly/cmd@v1.0.10/utils/command.go (about)

     1  package utils
     2  
     3  import (
     4  	"go/build"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  	"bytes"
     9  	"path/filepath"
    10  )
    11  
    12  // Initialize the command based on the GO environment
    13  func CmdInit(c *exec.Cmd, addGoPath bool, basePath string) {
    14  	c.Dir = basePath
    15  	// Dep does not like paths that are not real, convert all paths in go to real paths
    16  	realPath := &bytes.Buffer{}
    17  	if addGoPath {
    18  		for _, p := range filepath.SplitList(build.Default.GOPATH) {
    19  			rp, _ := filepath.EvalSymlinks(p)
    20  			if realPath.Len() > 0 {
    21  				realPath.WriteString(string(filepath.ListSeparator))
    22  			}
    23  			realPath.WriteString(rp)
    24  		}
    25  		// Go 1.8 fails if we do not include the GOROOT
    26  		c.Env = []string{"GOPATH=" + realPath.String(), "GOROOT=" + os.Getenv("GOROOT")}
    27  	}
    28  	// Fetch the rest of the env variables
    29  	for _, e := range os.Environ() {
    30  		pair := strings.Split(e, "=")
    31  		if pair[0] == "GOPATH" || pair[0] == "GOROOT" {
    32  			continue
    33  		}
    34  		c.Env = append(c.Env, e)
    35  	}
    36  }