-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.go
163 lines (146 loc) · 3.76 KB
/
list.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
// Copyright (c) 2018, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"strings"
)
// listPackages is akin to go/packages, but more specific to `go list`.
// In particular, it helps us reach fields like Dir and Deps.
// Moreover, by using `go test`, we are tightly coupled with cmd/go already.
func listPackages(args, flags []string) ([]*Package, error) {
// Load the packages.
var pkgs []*Package
listArgs := []string{"list", "-json"}
listArgs = append(listArgs, flags...)
listArgs = append(listArgs, args...)
cmd := exec.Command("go", listArgs...)
pr, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("list: %w", err)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("list: %w", err)
}
dec := json.NewDecoder(pr)
for {
var pkg Package
if err := dec.Decode(&pkg); err == io.EOF {
break
} else if err != nil {
return nil, fmt.Errorf("list: %w", err)
}
pkgs = append(pkgs, &pkg)
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("list: %v:\n%s", err, stderr.Bytes())
}
return pkgs, nil
}
type Package struct {
Dir string
ImportPath string
Name string
GoFiles []string
TestGoFiles []string
XTestGoFiles []string
Deps []string
}
// The code below was initially borrowed from github.com/burrowers/garble,
// which has to pass flags down to cmd/go in a very similar way.
// forwardBuildFlags is obtained from 'go help build' as of Go 1.18beta1.
var forwardBuildFlags = map[string]bool{
// These shouldn't be used in nested cmd/go calls.
"a": false,
"n": false,
"x": false,
"v": false,
"asan": true,
"asmflags": true,
"buildmode": true,
"buildvcs": true,
"compiler": true,
"gccgoflags": true,
"gcflags": true,
"installsuffix": true,
"ldflags": true,
"linkshared": true,
"mod": true,
"modcacherw": true,
"modfile": true,
"msan": true,
"overlay": true,
"p": true,
"pkgdir": true,
"race": true,
"tags": true,
"toolexec": true,
"trimpath": true,
"work": true,
"workfile": true,
}
// booleanFlags is obtained from 'go help build' and 'go help testflag' as of Go 1.21.
var booleanFlags = map[string]bool{
// Global help.
"h": true,
// Shared build flags.
"a": true,
"i": true,
"n": true,
"v": true,
"work": true,
"x": true,
"race": true,
"msan": true,
"asan": true,
"linkshared": true,
"modcacherw": true,
"trimpath": true,
"buildvcs": true,
// Test flags (TODO: support its special -args flag)
"c": true,
"json": true,
"cover": true,
"failfast": true,
"short": true,
"benchmem": true,
"fullpath": true,
// benchinit flags
"r": true,
}
func filterFlags(flags []string) (build, test, rest []string) {
for i := 0; i < len(flags); i++ {
arg := flags[i]
if !strings.HasPrefix(arg, "-") {
return build, test, append(rest, flags[i:]...)
}
arg = strings.TrimLeft(arg, "-") // `-name` or `--name` to `name`
name, _, _ := strings.Cut(arg, "=") // `name=value` to `name`
start := i
if strings.Contains(arg, "=") {
// `-flag=value`
} else if booleanFlags[name] {
// `-boolflag`
} else {
// `-flag value`
if i+1 < len(flags) {
i++
}
}
toAppend := flags[start : i+1]
if forwardBuildFlags[name] { // build flag
build = append(build, toAppend...)
} else if name == "h" || flagSet.Lookup(name) != nil { // benchinit flag
rest = append(rest, toAppend...)
} else { // by elimination, a test flag
test = append(test, toAppend...)
}
}
return build, test, rest
}