support goproxy for list/download command (#1304)

This commit is contained in:
Yi Tang
2019-07-16 02:07:24 +08:00
committed by Marwan Sulaiman
parent 8e8e886f84
commit 7de77889ad
9 changed files with 36 additions and 17 deletions
+9 -5
View File
@@ -20,6 +20,7 @@ import (
type goGetFetcher struct {
fs afero.Fs
goBinaryName string
goProxy string
}
type goModule struct {
@@ -35,7 +36,7 @@ type goModule struct {
}
// NewGoGetFetcher creates fetcher which uses go get tool to fetch modules
func NewGoGetFetcher(goBinaryName string, fs afero.Fs) (Fetcher, error) {
func NewGoGetFetcher(goBinaryName string, goProxy string, fs afero.Fs) (Fetcher, error) {
const op errors.Op = "module.NewGoGetFetcher"
if err := validGoBinary(goBinaryName); err != nil {
return nil, errors.E(op, err)
@@ -43,6 +44,7 @@ func NewGoGetFetcher(goBinaryName string, fs afero.Fs) (Fetcher, error) {
return &goGetFetcher{
fs: fs,
goBinaryName: goBinaryName,
goProxy: goProxy,
}, nil
}
@@ -65,7 +67,7 @@ func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Ver
return nil, errors.E(op, err)
}
m, err := downloadModule(g.goBinaryName, g.fs, goPathRoot, modPath, mod, ver)
m, err := downloadModule(g.goBinaryName, g.goProxy, g.fs, goPathRoot, modPath, mod, ver)
if err != nil {
ClearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
@@ -100,13 +102,13 @@ func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Ver
// given a filesystem, gopath, repository root, module and version, runs 'go mod download -json'
// on module@version from the repoRoot with GOPATH=gopath, and returns a non-nil error if anything went wrong.
func downloadModule(goBinaryName string, fs afero.Fs, gopath, repoRoot, module, version string) (goModule, error) {
func downloadModule(goBinaryName, goProxy string, fs afero.Fs, gopath, repoRoot, module, version string) (goModule, error) {
const op errors.Op = "module.downloadModule"
uri := strings.TrimSuffix(module, "/")
fullURI := fmt.Sprintf("%s@%s", uri, version)
cmd := exec.Command(goBinaryName, "mod", "download", "-json", fullURI)
cmd.Env = PrepareEnv(gopath)
cmd.Env = PrepareEnv(gopath, goProxy)
cmd.Dir = repoRoot
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
@@ -140,7 +142,7 @@ func downloadModule(goBinaryName string, fs afero.Fs, gopath, repoRoot, module,
// PrepareEnv will return all the appropriate
// environment variables for a Go Command to run
// successfully (such as GOPATH, GOCACHE, PATH etc)
func PrepareEnv(gopath string) []string {
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"))
@@ -151,6 +153,7 @@ func PrepareEnv(gopath string) []string {
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"))
@@ -160,6 +163,7 @@ func PrepareEnv(gopath string) []string {
pathEnv,
homeEnv,
gopathEnv,
goProxyEnv,
cacheEnv,
disableCgo,
enableGoModules,