/
gocommand.go
253 lines (234 loc) · 6.55 KB
/
gocommand.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package buildutil
import (
"context"
"go/build"
"os/exec"
"strings"
"github.com/charlievieth/buildutil/internal/util"
)
// GoCommandContext returns an exec.Cmd for the provided build.Context and
// context.Context. The Cmd's env is set to that of the Context. The args
// contains a "-tags" flag it is updated to match the build constraints of
// the Context otherwise the "-tags" are provided via the GOFLAGS env var.
func GoCommandContext(ctx context.Context, ctxt *build.Context, name string, args ...string) *exec.Cmd {
if ctxt == nil {
orig := build.Default
ctxt = &orig
}
e := util.NewEnviron()
e.Set("GOPATH", ctxt.GOPATH)
if s, _ := e.Lookup("GOROOT"); s != "" && s != ctxt.GOROOT {
e.Set("GOROOT", ctxt.GOROOT)
}
if ctxt.GOOS != "" {
e.Set("GOOS", ctxt.GOOS)
}
if ctxt.GOARCH != "" {
e.Set("GOARCH", ctxt.GOARCH)
}
if ctxt.CgoEnabled {
e.Set("CGO_ENABLED", "1")
} else {
e.Set("CGO_ENABLED", "0")
}
if len(ctxt.ToolTags) != 0 {
a := make([]string, 0, len(ctxt.ToolTags))
for _, s := range ctxt.ToolTags {
if strings.HasPrefix(s, "goexperiment.") {
a = append(a, strings.TrimPrefix(s, "goexperiment."))
}
}
e.Set("GOEXPERIMENT", strings.Join(a, ","))
}
if len(ctxt.BuildTags) != 0 {
// Command line arguments take precedence over the GOFLAGS
// environment variable so we have to update the "-tags"
// argument, if provided.
existingTags := extractTagArgs(args)
if len(existingTags) != 0 {
args = replaceTagArgs(args, mergeTagArgs(existingTags, ctxt.BuildTags))
} else {
if s, _ := e.Lookup("GOFLAGS"); s != "" {
// TODO: check if "-tags" is already defined
e.Set("GOFLAGS", s+" -tags="+strings.Join(ctxt.BuildTags, ","))
} else {
e.Set("GOFLAGS", "-tags="+strings.Join(ctxt.BuildTags, ","))
}
}
}
cmd := exec.CommandContext(ctx, name, args...)
cmd.Env = e.Environ()
return cmd
///////////////////////////////////////////
// e := env(os.Environ())
// e = e.Set("GOPATH", ctxt.GOPATH)
// if s, _ := e.Lookup("GOROOT"); s != "" && s != ctxt.GOROOT {
// e = e.Set("GOROOT", ctxt.GOROOT)
// }
// if ctxt.GOOS != "" {
// e = e.Set("GOOS", ctxt.GOOS)
// }
// if ctxt.GOARCH != "" {
// e = e.Set("GOARCH", ctxt.GOARCH)
// }
// if ctxt.CgoEnabled {
// e = e.Set("CGO_ENABLED", "1")
// } else {
// e = e.Set("CGO_ENABLED", "0")
// }
// if len(ctxt.ToolTags) != 0 {
// a := make([]string, 0, len(ctxt.ToolTags))
// for _, s := range ctxt.ToolTags {
// if strings.HasPrefix(s, "goexperiment.") {
// a = append(a, strings.TrimPrefix(s, "goexperiment."))
// }
// }
// e = e.Set("GOEXPERIMENT", strings.Join(a, ","))
// }
// if len(ctxt.BuildTags) != 0 {
// // Command line arguments take precedence over the GOFLAGS
// // environment variable so we have to update the "-tags"
// // argument, if provided.
// existingTags := extractTagArgs(args)
// if len(existingTags) != 0 {
// args = replaceTagArgs(args, mergeTagArgs(existingTags, ctxt.BuildTags))
// } else {
// if s, _ := e.Lookup("GOFLAGS"); s != "" {
// // TODO: check if "-tags" is already defined
// e = e.Set("GOFLAGS", s+" -tags="+strings.Join(ctxt.BuildTags, ","))
// } else {
// e = e.Set("GOFLAGS", "-tags="+strings.Join(ctxt.BuildTags, ","))
// }
// }
// }
// cmd := exec.CommandContext(ctx, name, args...)
// cmd.Env = e.Value()
// return cmd
///////////////////////////////////////////
// m := envMap(os.Environ())
// m["GOPATH"] = ctxt.GOPATH
// if s := m["GOROOT"]; s != "" && s != ctxt.GOROOT {
// m["GOROOT"] = ctxt.GOROOT
// }
// if ctxt.GOOS != "" {
// m["GOOS"] = ctxt.GOOS
// }
// if ctxt.GOARCH != "" {
// m["GOARCH"] = ctxt.GOARCH
// }
// if ctxt.CgoEnabled {
// m["CGO_ENABLED"] = "1"
// } else {
// m["CGO_ENABLED"] = "0"
// }
// if len(ctxt.ToolTags) != 0 {
// a := make([]string, 0, len(ctxt.ToolTags))
// for _, s := range ctxt.ToolTags {
// if strings.HasPrefix(s, "goexperiment.") {
// a = append(a, strings.TrimPrefix(s, "goexperiment."))
// }
// }
// m["GOEXPERIMENT"] = strings.Join(a, ",")
// }
//
// if len(ctxt.BuildTags) != 0 {
// // Command line arguments take precedence over the GOFLAGS
// // environment variable so we have to update the "-tags"
// // argument, if provided.
// existingTags := extractTagArgs(args)
// if len(existingTags) != 0 {
// args = replaceTagArgs(args, mergeTagArgs(existingTags, ctxt.BuildTags))
// } else {
// if s := m["GOFLAGS"]; s != "" {
// // TODO: check if "-tags" is already defined
// m["GOFLAGS"] = s + " -tags=" + strings.Join(ctxt.BuildTags, ",")
// } else {
// m["GOFLAGS"] = "-tags=" + strings.Join(ctxt.BuildTags, ",")
// }
// }
// }
//
// env := make([]string, len(m))
// for k, v := range m {
// env = append(env, k+"="+v)
// }
//
// cmd := exec.CommandContext(ctx, name, args...)
// cmd.Env = env
//
// return cmd
}
// GoCommand returns an exec.Cmd for the provided build.Context. The Cmd's
// env is set to that of the Context. The args contains a "-tags" flag it
// is updated to match the build constraints of the Context otherwise the
// "-tags" are provided via the GOFLAGS env var.
func GoCommand(ctxt *build.Context, name string, args ...string) *exec.Cmd {
return GoCommandContext(context.Background(), ctxt, name, args...)
}
func envMap(a []string) map[string]string {
m := make(map[string]string, len(a))
for _, s := range a {
k, v, _ := cut(s, "=")
m[k] = v
}
return m
}
func mergeTagArgs(old, new []string) []string {
if len(old) == 0 {
return new
}
if len(new) == 0 {
return old
}
var args []string
Loop:
for _, arg := range old {
s := strings.TrimPrefix(arg, "!")
for _, x := range new {
if s == strings.TrimPrefix(x, "!") {
continue Loop
}
}
args = append(args, arg)
}
return append(args, new...)
}
func extractTagArgs(args []string) []string {
for i := 0; i < len(args); i++ {
switch s := args[i]; {
case s == "--":
// stop parsing args
return nil
case s == "-tags":
if i < len(args)-1 {
return strings.Split(args[i+1], ",")
}
// invalid -tags argument (ignore)
return nil
case strings.HasPrefix(s, "-tags="):
return strings.Split(strings.TrimPrefix(s, "-tags="), ",")
}
}
return nil
}
func replaceTagArgs(args, tags []string) []string {
a := make([]string, len(args))
copy(a, args)
for i := 0; i < len(a); i++ {
s := a[i]
if s == "--" {
break // stop parsing args
}
if s == "-tags" {
if i < len(a)-1 {
a[i+1] = strings.Join(tags, ",")
}
break
}
if strings.HasPrefix(s, "-tags=") {
a[i] = "-tags=" + strings.Join(tags, ",")
break
}
}
return a
}