-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.go
168 lines (129 loc) · 2.98 KB
/
package.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
package main
import (
"encoding/json"
"io"
"os"
"os/exec"
"sort"
)
const packageBatchSize = 100
// via go list -json
type Package struct {
Standard bool
ImportPath string
Deps []string
TestImports []string
XTestImports []string
}
func listPackages(ps ...string) ([]Package, error) {
if len(ps) == 0 {
return []Package{}, nil
}
pkgs := map[string]Package{}
packages := []string{}
remainingPackages := ps
for {
if len(remainingPackages) == 0 {
break
}
if len(remainingPackages) < packageBatchSize {
packages = remainingPackages
remainingPackages = nil
} else {
packages = remainingPackages[:packageBatchSize]
remainingPackages = remainingPackages[packageBatchSize:]
}
listPackages := exec.Command(
"go",
append([]string{"list", "-e", "-json"}, packages...)...,
)
listPackages.Stderr = os.Stderr
packageStream, err := listPackages.StdoutPipe()
if err != nil {
return nil, err
}
err = listPackages.Start()
if err != nil {
return nil, err
}
decoder := json.NewDecoder(packageStream)
for {
var pkg Package
err := decoder.Decode(&pkg)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
pkgs[pkg.ImportPath] = pkg
}
err = listPackages.Wait()
if err != nil {
return nil, err
}
}
pkgList := []Package{}
for _, pkg := range pkgs {
pkgList = append(pkgList, pkg)
}
sort.Sort(byImportPath(pkgList))
return pkgList, nil
}
type byImportPath []Package
func (ps byImportPath) Len() int { return len(ps) }
func (ps byImportPath) Less(i int, j int) bool { return ps[i].ImportPath < ps[j].ImportPath }
func (ps byImportPath) Swap(i int, j int) { ps[i], ps[j] = ps[j], ps[i] }
func getAppImports(packages ...string) ([]string, error) {
appPackages, err := listPackages(packages...)
if err != nil {
return nil, err
}
imports := []string{}
for _, pkg := range appPackages {
imports = append(imports, pkg.ImportPath)
}
return imports, nil
}
func getTestImports(packages ...string) ([]string, error) {
testPackages, err := listPackages(packages...)
if err != nil {
return nil, err
}
imports := []string{}
imports = append(imports, packages...)
for _, pkg := range testPackages {
imports = append(imports, pkg.TestImports...)
imports = append(imports, pkg.XTestImports...)
}
return filterNonStandard(imports...)
}
func getAllDeps(packages ...string) ([]string, error) {
pkgs, err := listPackages(packages...)
if err != nil {
return nil, err
}
allDeps := []string{}
allDeps = append(allDeps, packages...)
for _, pkg := range pkgs {
if pkg.Standard {
continue
}
allDeps = append(allDeps, pkg.Deps...)
}
return allDeps, nil
}
func filterNonStandard(packages ...string) ([]string, error) {
pkgs, err := listPackages(packages...)
if err != nil {
return nil, err
}
nonStandard := []string{}
for _, pkg := range pkgs {
if pkg.Standard {
continue
}
nonStandard = append(nonStandard, pkg.ImportPath)
}
return nonStandard, nil
}