mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
pkg/module: set only present environment variables (#1399)
* pkg/module: set only present environment variables * pkg/module: apply new env variable logic to all keys
This commit is contained in:
committed by
Aaron Schlesinger
parent
b325db64bb
commit
790c74588a
+29
-35
@@ -11,43 +11,47 @@ 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,
|
||||
}
|
||||
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 runtime.GOOS == "windows" {
|
||||
windowsSpecificKeys := []string{
|
||||
"USERPROFILE",
|
||||
"SystemRoot",
|
||||
"ALLUSERSPROFILE",
|
||||
"HOMEDRIVE",
|
||||
"HOMEPATH",
|
||||
}
|
||||
keys = append(keys, windowsSpecificKeys...)
|
||||
}
|
||||
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...)
|
||||
|
||||
// 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))
|
||||
}
|
||||
if httpsProxyLower, exist := os.LookupEnv("https_proxy"); exist {
|
||||
cmdEnv = append(cmdEnv, fmt.Sprintf("https_proxy=%s", httpsProxyLower))
|
||||
}
|
||||
if noProxyLower, exist := os.LookupEnv("no_proxy"); exist {
|
||||
cmdEnv = append(cmdEnv, fmt.Sprintf("no_proxy=%s", noProxyLower))
|
||||
}
|
||||
|
||||
if sshAuthSockVal, hasSSHAuthSock := os.LookupEnv("SSH_AUTH_SOCK"); hasSSHAuthSock {
|
||||
// Verify that the ssh agent unix socket exists and is a unix socket.
|
||||
st, err := os.Stat(sshAuthSockVal)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user