-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand.go
More file actions
84 lines (76 loc) · 2.14 KB
/
command.go
File metadata and controls
84 lines (76 loc) · 2.14 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"log/slog"
"os/exec"
"path/filepath"
"runtime"
)
// We allow one concurrent non-build commands, e.g. for fetching modules,
// listing main commands in a module, checking for cgo. When executing a command
// based on a web request, a command acquire token is required. Full builds are
// already managed by the coordinator and its command executions must not get a
// token.
// Once we isolate builds more properly, we can increase concurrency again.
var cmdacquirec = make(chan struct{}, 1)
func init() {
// Fill with tokens.
for range cap(cmdacquirec) {
cmdacquirec <- struct{}{}
}
}
func commandAcquire(ctx context.Context) (release func(), err error) {
select {
case <-ctx.Done():
return func() {}, ctx.Err()
case <-cmdacquirec:
return func() {
cmdacquirec <- struct{}{}
}, nil
}
}
// Prepare command, typically for running go get. We sometimes need CGO_ENABLED to
// properly list the cgo files that would be used during a build. Only set
// withGoproxy for downloading modules, not doing builds or listing packages.
func makeCommand(goversion string, withGoproxy bool, dir string, cgoEnabled bool, extraEnv []string, argv ...string) *exec.Cmd {
cgo := "CGO_ENABLED=0"
if cgoEnabled {
cgo = "CGO_ENABLED=1"
}
goproxy := "GOPROXY=off"
if withGoproxy {
goproxy = "GOPROXY=" + config.GoProxy
}
var l []string
l = append(l, config.Run...)
l = append(l, argv...)
cmd := exec.Command(l[0], l[1:]...)
cmd.Dir = dir
cmd.Env = []string{
goproxy,
cgo,
"GO111MODULE=on",
"GO19CONCURRENTCOMPILATION=0",
"GOTOOLCHAIN=" + goversion,
}
switch runtime.GOOS {
case "windows":
cmd.Env = append(cmd.Env,
"USERPROFILE="+homedir,
"AppData="+filepath.Join(homedir, "AppData"),
"LocalAppData="+filepath.Join(homedir, "LocalAppData"),
)
case "plan9":
cmd.Env = append(cmd.Env, "home="+homedir)
default:
cmd.Env = append(cmd.Env, "HOME="+homedir)
}
if len(config.Environment) > 0 {
cmd.Env = append(cmd.Env, config.Environment...)
}
if len(extraEnv) > 0 {
cmd.Env = append(cmd.Env, extraEnv...)
}
slog.Debug("prepared command", "workdir", dir, "argv", l, "environment", cmd.Env)
return cmd
}