-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathimports.go
262 lines (206 loc) · 4.9 KB
/
imports.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
254
255
256
257
258
259
260
261
262
package main
import (
"encoding/json"
"errors"
"go/build"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/reconquest/karma-go"
)
type golistOutput struct {
Imports []string
Deps []string
TestImports []string
XTestImports []string
}
func parseImports(recursive bool, testDependencies bool) ([]string, error) {
var imports []string
packages, err := listPackages()
if err != nil {
return imports, karma.Format(
err, "unable to list packages",
)
}
// Ensuring our dependencies exists isn't a strict requirement, therefore
// only print a message to stderr rather then completely failing.
err = ensureDependenciesExist(packages, true)
if err != nil {
logger.Warning(err)
}
imports, err = calculateDependencies(packages, recursive, testDependencies)
if err != nil {
return imports, err
}
imports = filterPackages(imports, build.IgnoreVendor)
sort.Strings(imports)
return imports, nil
}
func calculateDependencies(
packages []string,
recursive,
testDependencies bool,
) ([]string, error) {
var deps, testImports, testDeps []string
data, err := golist(packages...)
if err != nil {
return nil, karma.Format(
err, "unable to list dependencies for some packages in: %v", packages,
)
}
for _, pkgMetaData := range data {
if recursive {
deps = append(deps, pkgMetaData.Deps...)
} else {
deps = append(deps, pkgMetaData.Imports...)
}
if testDependencies {
testImports = append(testImports, pkgMetaData.TestImports...)
testImports = append(testImports, pkgMetaData.XTestImports...)
}
}
testImports = unique(testImports)
if recursive {
testData, err := golist(testImports...)
if err != nil {
return nil, karma.Format(
err, "unable to list dependencies for some test packages in: %v", testImports)
}
for _, pkgMetaData := range testData {
testDeps = append(testDeps, pkgMetaData.Deps...)
}
deps = append(deps, testDeps...)
}
deps = append(deps, testImports...)
deps = unique(deps)
return deps, nil
}
func filterPackages(packages []string, mode build.ImportMode) []string {
var imports []string
for _, importing := range packages {
if importing == "C" {
continue
}
pkg, err := build.Import(importing, "", mode)
if err != nil {
continue
}
// must skip packages in GOROOT dirs
if pkg.Goroot {
continue
}
importpath, err := getRootImportpath(pkg, importing)
if err != nil {
continue
}
if testing {
importpath = strings.Replace(
importpath, "__blankd__", "localhost:60001", -1,
)
}
if isOwnPackage(importpath) {
continue
}
// Skip anything that is already vendored upstream
if strings.Contains(importpath, "/vendor/") {
continue
}
found := false
for _, imported := range imports {
if importpath == imported {
found = true
break
}
}
if found {
continue
}
imports = append(imports, importpath)
}
return imports
}
func ensureDependenciesExist(packages []string, withTests bool) error {
if packages == nil {
return errors.New("packages list cannot be empty")
}
args := []string{"get", "-d"} // -d for download only
if withTests {
args = append(args, "-t")
}
for _, pkg := range packages {
args = append(args, pkg)
}
_, err := execute(exec.Command("go", args...))
if err != nil {
return karma.Format(
err,
"unable to go get dependencies for one of the packages in %v",
packages,
)
}
return nil
}
func golist(packages ...string) ([]golistOutput, error) {
result := []golistOutput{}
if packages == nil {
return result, errors.New("packages list cannot be empty")
}
args := []string{"list", "-e", "-json"}
for _, pkg := range packages {
args = append(args, pkg)
}
jsonStream, err := execute(exec.Command("go", args...))
if err != nil {
return result, err
}
decoder := json.NewDecoder(strings.NewReader(jsonStream))
for {
pkgMetaData := golistOutput{}
err := decoder.Decode(&pkgMetaData)
if err != nil {
return result, karma.Format(err, "failed to decode go list output")
}
result = append(result, pkgMetaData)
if !decoder.More() {
break
}
}
return result, nil
}
func listPackages() ([]string, error) {
var packages []string
out, err := execute(exec.Command("go", "list", "-e", "./..."))
if err != nil {
return packages, err
}
for _, pkg := range strings.Split(strings.TrimSpace(out), "\n") {
if strings.Contains(pkg, "/vendor/") {
continue
}
packages = append(packages, pkg)
}
return packages, nil
}
func isOwnPackage(path string) bool {
for _, gopath := range filepath.SplitList(os.Getenv("GOPATH")) {
if strings.HasPrefix(filepath.Join(gopath, "src", path), workdir) {
return true
}
}
return false
}
func unique(input []string) []string {
var (
list = make([]string, 0, len(input))
table = make(map[string]bool)
)
for _, value := range input {
if _, ok := table[value]; !ok {
table[value] = true
list = append(list, value)
}
}
return list
}