Skip to content

Commit

Permalink
pkg/module: set only present environment variables (#1399)
Browse files Browse the repository at this point in the history
* pkg/module: set only present environment variables

* pkg/module: apply new env variable logic to all keys
  • Loading branch information
ernado authored and arschles committed Oct 16, 2019
1 parent b325db6 commit 790c745
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions pkg/module/prepare_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,46 @@ import (
// environment variables for a Go Command to run
// successfully (such as GOPATH, GOCACHE, PATH etc)
func prepareEnv(gopath string, envVars []string) []string {
pathEnv := fmt.Sprintf("PATH=%s", os.Getenv("PATH"))
homeEnv := fmt.Sprintf("HOME=%s", os.Getenv("HOME"))
httpProxy := fmt.Sprintf("HTTP_PROXY=%s", os.Getenv("HTTP_PROXY"))
httpsProxy := fmt.Sprintf("HTTPS_PROXY=%s", os.Getenv("HTTPS_PROXY"))
noProxy := fmt.Sprintf("NO_PROXY=%s", os.Getenv("NO_PROXY"))
gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
cacheEnv := fmt.Sprintf("GOCACHE=%s", filepath.Join(gopath, "cache"))
gitSSH := fmt.Sprintf("GIT_SSH=%s", os.Getenv("GIT_SSH"))
gitSSHCmd := fmt.Sprintf("GIT_SSH_COMMAND=%s", os.Getenv("GIT_SSH_COMMAND"))
disableCgo := "CGO_ENABLED=0"
enableGoModules := "GO111MODULE=on"
cmdEnv := []string{
pathEnv,
homeEnv,
gopathEnv,
cacheEnv,
disableCgo,
enableGoModules,
httpProxy,
httpsProxy,
noProxy,
gitSSH,
gitSSHCmd,
}
cmdEnv = append(cmdEnv, envVars...)

// need to also check the lower case version of just these three env variables
if httpProxyLower, exist := os.LookupEnv("http_proxy"); exist {
cmdEnv = append(cmdEnv, fmt.Sprintf("http_proxy=%s", httpProxyLower))
keys := []string{
"PATH",
"HOME",
"GIT_SSH",
"GIT_SSH_COMMAND",
"HTTP_PROXY",
"HTTPS_PROXY",
"NO_PROXY",
// Need to also check the lower case version of just these three env variables.
"http_proxy",
"https_proxy",
"no_proxy",
}
if httpsProxyLower, exist := os.LookupEnv("https_proxy"); exist {
cmdEnv = append(cmdEnv, fmt.Sprintf("https_proxy=%s", httpsProxyLower))
if runtime.GOOS == "windows" {
windowsSpecificKeys := []string{
"USERPROFILE",
"SystemRoot",
"ALLUSERSPROFILE",
"HOMEDRIVE",
"HOMEPATH",
}
keys = append(keys, windowsSpecificKeys...)
}
if noProxyLower, exist := os.LookupEnv("no_proxy"); exist {
cmdEnv = append(cmdEnv, fmt.Sprintf("no_proxy=%s", noProxyLower))
for _, key := range keys {
// Prepend only if environment variable is present.
if v, ok := os.LookupEnv(key); ok {
cmdEnv = append(cmdEnv, fmt.Sprintf("%s=%s", key, v))
}
}
cmdEnv = append(cmdEnv, envVars...)

if sshAuthSockVal, hasSSHAuthSock := os.LookupEnv("SSH_AUTH_SOCK"); hasSSHAuthSock {
// Verify that the ssh agent unix socket exists and is a unix socket.
Expand All @@ -56,15 +60,5 @@ func prepareEnv(gopath string, envVars []string) []string {
cmdEnv = append(cmdEnv, sshAuthSock)
}
}

// add Windows specific ENV VARS
if runtime.GOOS == "windows" {
cmdEnv = append(cmdEnv, fmt.Sprintf("USERPROFILE=%s", os.Getenv("USERPROFILE")))
cmdEnv = append(cmdEnv, fmt.Sprintf("SystemRoot=%s", os.Getenv("SystemRoot")))
cmdEnv = append(cmdEnv, fmt.Sprintf("ALLUSERSPROFILE=%s", os.Getenv("ALLUSERSPROFILE")))
cmdEnv = append(cmdEnv, fmt.Sprintf("HOMEDRIVE=%s", os.Getenv("HOMEDRIVE")))
cmdEnv = append(cmdEnv, fmt.Sprintf("HOMEPATH=%s", os.Getenv("HOMEPATH")))
}

return cmdEnv
}

0 comments on commit 790c745

Please sign in to comment.