Fix running the athens proxy on windows (#554)

* use parent env

* rename

* use only required vars

* review feedback

* add windows env vars

* ups

* fix

* rm op
This commit is contained in:
marpio
2018-08-24 07:55:55 +02:00
committed by Marwan Sulaiman
parent e5ddbedfa8
commit 76121fb5cb
+19 -9
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/gomods/athens/pkg/errors"
@@ -83,18 +84,10 @@ func Dummy(fs afero.Fs, repoRoot string) error {
func getSources(goBinaryName string, fs afero.Fs, gopath, repoRoot, module, version string) error {
const op errors.Op = "module.getSources"
uri := strings.TrimSuffix(module, "/")
fullURI := fmt.Sprintf("%s@%s", uri, version)
gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
cacheEnv := fmt.Sprintf("GOCACHE=%s", filepath.Join(gopath, "cache"))
disableCgo := "CGO_ENABLED=0"
enableGoModules := "GO111MODULE=on"
cmd := exec.Command(goBinaryName, "mod", "download", fullURI)
// PATH is needed for vgo to recognize vcs binaries
// this breaks windows.
cmd.Env = []string{"PATH=" + os.Getenv("PATH"), gopathEnv, cacheEnv, disableCgo, enableGoModules}
cmd.Env = prepareEnv(gopath)
cmd.Dir = repoRoot
o, err := cmd.CombinedOutput()
if err != nil {
@@ -120,6 +113,23 @@ func getSources(goBinaryName string, fs afero.Fs, gopath, repoRoot, module, vers
return nil
}
func prepareEnv(gopath string) []string {
pathEnv := fmt.Sprintf("PATH=%s", os.Getenv("PATH"))
gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
cacheEnv := fmt.Sprintf("GOCACHE=%s", filepath.Join(gopath, "cache"))
disableCgo := "CGO_ENABLED=0"
enableGoModules := "GO111MODULE=on"
cmdEnv := []string{pathEnv, gopathEnv, cacheEnv, disableCgo, enableGoModules}
// 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")))
}
return cmdEnv
}
func checkFiles(fs afero.Fs, path, version string) error {
const op errors.Op = "module.checkFiles"
if _, err := fs.Stat(filepath.Join(path, version+".mod")); err != nil {