-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-goenv.patch
65 lines (61 loc) · 1.56 KB
/
01-goenv.patch
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
commit 33fda521f72037473a9153266a28e2dedc510f02
Author: phus.lu <phus.lu@gmail.com>
Date: 2022-06-17 23:49:02 +0800
go/build: add go.env support
diff --git a/src/go/build/build.go b/src/go/build/build.go
index dd6cdc903a..735d93c27d 100644
--- a/src/go/build/build.go
+++ b/src/go/build/build.go
@@ -352,6 +352,54 @@ func defaultContext() Context {
c.CgoEnabled = false
}
+ if os.Getenv("GOENV_ENABLED") != "0" {
+ dir, _ := os.Getwd()
+ var lines []string
+ for i := 0; i < 9; i++ {
+ if data, _ := os.ReadFile(filepath.Join(dir, "go.env")); len(data) != 0 {
+ lines = strings.Split(string(data), "\n")
+ break
+ }
+ if fi, _ := os.Stat(filepath.Join(dir, ".git")); fi != nil && fi.IsDir() {
+ break
+ }
+ if dir = filepath.Dir(dir); dir == "/" {
+ break
+ }
+ }
+ for _, line := range lines {
+ parts := strings.SplitN(line, "=", 2)
+ if len(parts) == 1 || strings.HasPrefix(strings.TrimSpace(line), "#") {
+ continue
+ }
+ key := strings.TrimSpace(parts[0])
+ value := strings.TrimSpace(parts[1])
+ os.Setenv(key, value)
+ switch key {
+ case "CGO_ENABLED":
+ switch value {
+ case "1":
+ c.CgoEnabled = true
+ case "0":
+ c.CgoEnabled = false
+ }
+ case "GOOS":
+ c.GOOS = value
+ case "GOARCH":
+ c.GOARCH = value
+ case "GOPATH":
+ c.GOPATH = value
+ case "GOROOT":
+ c.GOROOT = value
+ case "http_proxy":
+ os.Setenv("http_proxy", value)
+ os.Setenv("https_proxy", value)
+ os.Setenv("HTTP_PROXY", value)
+ os.Setenv("HTTPS_PROXY", value)
+ }
+ }
+ }
+
return c
}