/
gopath.go
69 lines (58 loc) · 1.64 KB
/
gopath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gopath
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/uwu-tools/magex/xplat"
)
// EnsureGopathBin ensures that GOPATH/bin exists and is in PATH.
// Detects if this is an Azure CI build and exports the updated PATH.
func EnsureGopathBin() error {
gopathBin := GetGopathBin()
err := os.MkdirAll(gopathBin, 0755)
if err != nil {
return fmt.Errorf("could not create GOPATH/bin at %s: %w", gopathBin, err)
}
xplat.EnsureInPath(GetGopathBin())
return nil
}
// GetGopathBin returns GOPATH/bin.
func GetGopathBin() string {
return filepath.Join(GOPATH(), "bin")
}
// GOPATH returns the current GOPATH.
func GOPATH() string {
gopath := os.Getenv("GOPATH")
if gopath != "" {
return gopath
}
return build.Default.GOPATH
}
// UseTempGopath sets the current GOPATH environment variable to a temporary
// directory, returning a cleanup function which reverts the change.
func UseTempGopath() (error, func()) {
oldpath := os.Getenv("PATH")
tmp, err := ioutil.TempDir("", "magex")
if err != nil {
return fmt.Errorf("failed to create a temp directory: %w", err), func() {}
}
cleanup := func() {
os.RemoveAll(tmp)
defer os.Setenv("PATH", oldpath)
defer os.Setenv("GOPATH", build.Default.GOPATH)
}
// Remove actual GOPATH/bin from PATH so the test doesn't accidentally pass because the package was installed before the test was run
gopathBin := filepath.Join(build.Default.GOPATH, "bin")
os.Setenv("PATH", strings.ReplaceAll(oldpath, gopathBin, ""))
// Use temp dir for GOPATH
os.Setenv("GOPATH", tmp)
err = EnsureGopathBin()
if err != nil {
cleanup()
return err, nil
}
return nil, cleanup
}