ref: upstream lister and it's impl to module pkg (#1309)

This commit is contained in:
Yi Tang
2019-07-26 08:40:03 +08:00
committed by Marwan Sulaiman
parent 5bff5fe74d
commit ca10728887
8 changed files with 94 additions and 80 deletions
+3 -63
View File
@@ -8,7 +8,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/gomods/athens/pkg/errors"
@@ -63,13 +62,13 @@ func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Ver
sourcePath := filepath.Join(goPathRoot, "src")
modPath := filepath.Join(sourcePath, getRepoDirName(mod, ver))
if err := g.fs.MkdirAll(modPath, os.ModeDir|os.ModePerm); err != nil {
ClearFiles(g.fs, goPathRoot)
clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
m, err := downloadModule(g.goBinaryName, g.goProxy, g.fs, goPathRoot, modPath, mod, ver)
if err != nil {
ClearFiles(g.fs, goPathRoot)
clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
@@ -108,7 +107,7 @@ func downloadModule(goBinaryName, goProxy string, fs afero.Fs, gopath, repoRoot,
fullURI := fmt.Sprintf("%s@%s", uri, version)
cmd := exec.Command(goBinaryName, "mod", "download", "-json", fullURI)
cmd.Env = PrepareEnv(gopath, goProxy)
cmd.Env = prepareEnv(gopath, goProxy)
cmd.Dir = repoRoot
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
@@ -139,65 +138,6 @@ func downloadModule(goBinaryName, goProxy string, fs afero.Fs, gopath, repoRoot,
return m, nil
}
// PrepareEnv will return all the appropriate
// environment variables for a Go Command to run
// successfully (such as GOPATH, GOCACHE, PATH etc)
func PrepareEnv(gopath, goProxy 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"))
// need to also check the lower case version of just these three env variables
httpProxyLower := fmt.Sprintf("http_proxy=%s", os.Getenv("http_proxy"))
httpsProxyLower := fmt.Sprintf("https_proxy=%s", os.Getenv("https_proxy"))
noProxyLower := fmt.Sprintf("no_proxy=%s", os.Getenv("no_proxy"))
gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
goProxyEnv := fmt.Sprintf("GOPROXY=%s", goProxy)
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,
goProxyEnv,
cacheEnv,
disableCgo,
enableGoModules,
httpProxy,
httpsProxy,
noProxy,
httpProxyLower,
httpsProxyLower,
noProxyLower,
gitSSH,
gitSSHCmd,
}
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)
if err == nil && st.Mode()&os.ModeSocket != 0 {
sshAuthSock := fmt.Sprintf("SSH_AUTH_SOCK=%s", sshAuthSockVal)
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
}
func isLimitHit(o string) bool {
return strings.Contains(o, "403 response from api.github.com")
}