-
Notifications
You must be signed in to change notification settings - Fork 6
/
vendor.go
336 lines (294 loc) · 7.63 KB
/
vendor.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
var (
logfile = flag.String("log", "vendor-log", "file `name` for list of commit ids")
)
type Package struct {
Dir string // directory containing package sources
ImportPath string // import path of package in dir
ImportComment string // path in import comment on package statement
Name string // package name
Doc string // package documentation string
Target string // install path
Shlib string // the shared library that contains this package (only set when -linkshared)
Goroot bool // is this package in the Go root?
Standard bool // is this package part of the standard Go library?
Stale bool // would 'go install' do anything for this package?
Root string // Go root or Go path dir containing this package
// Source files
GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
CgoFiles []string // .go sources files that import "C"
IgnoredGoFiles []string // .go sources ignored due to build constraints
CFiles []string // .c source files
CXXFiles []string // .cc, .cxx and .cpp source files
MFiles []string // .m source files
HFiles []string // .h, .hh, .hpp and .hxx source files
SFiles []string // .s source files
SwigFiles []string // .swig files
SwigCXXFiles []string // .swigcxx files
SysoFiles []string // .syso object files to add to archive
// Cgo directives
CgoCFLAGS []string // cgo: flags for C compiler
CgoCPPFLAGS []string // cgo: flags for C preprocessor
CgoCXXFLAGS []string // cgo: flags for C++ compiler
CgoLDFLAGS []string // cgo: flags for linker
CgoPkgConfig []string // cgo: pkg-config names
// Dependency information
Imports []string // import paths used by this package
Deps []string // all (recursively) imported dependencies
// Error information
Incomplete bool // this package or a dependency has an error
Error *PackageError // error loading package
DepsErrors []*PackageError // errors loading dependencies
TestGoFiles []string // _test.go files in package
TestImports []string // imports from TestGoFiles
XTestGoFiles []string // _test.go files outside package
XTestImports []string // imports from XTestGoFiles
}
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one
Pos string // position of error (if present, file:line:col)
Err string // the error itself
}
const usage = `Vendor is a tool for managing Go dependencies.
Usage:
vendor fileOrpkg...
Examples:
vendor .
vendor ./cmd/acommand
vendor main.go
vendor github.com/lib/pq
Running vendor with a package name will install the package and its
dependencies into ./vendor if and only if those packages are not local to the
current directory.
Running vendor on a file will vendor the file if it is not local along with the
files non-local dependencies.
Vendor will skip a file or packages vendored dependencies and print them to
stdout for you to decide what to do about them. Usually running vendor again
with their package name minus the github.com/want/thing/vendor/ prefix is
ideal.
Reverting changes made by vendor are done with a scm like Git.
Vendor will append all packages vendored and their Git SHAs to ./vendor-log for
your auditing and version tracking purposes.
`
func main() {
flag.Usage = func() {
io.WriteString(os.Stdout, usage)
os.Exit(0)
}
log.SetFlags(0)
log.SetPrefix("vendor: ")
flag.Parse()
vendor(flag.Args(), true)
reportExtVendoredDep()
err := reportManifest(*logfile)
if err != nil {
log.Print(err)
}
}
var extVendoredDeps map[string]bool
func noteExtVendoredDep(p *Package) {
if extVendoredDeps == nil {
extVendoredDeps = make(map[string]bool)
}
path := p.ImportPath
if extVendoredDeps[path] {
return
}
extVendoredDeps[path] = true
}
func reportExtVendoredDep() {
for k, _ := range extVendoredDeps {
_, err := os.Stat(filepath.Join(getwd(), "vendor", k))
if err != nil {
if os.IsNotExist(err) {
fmt.Println(k)
continue
}
log.Fatal(err)
}
}
}
var manifest = map[string]*Package{}
func noteManifest(p *Package) {
manifest[p.ImportPath] = p
}
func reportManifest(name string) error {
var imps []string
for imp := range manifest {
imps = append(imps, imp)
}
sort.Strings(imps)
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
w := bufio.NewWriter(f)
for _, imp := range imps {
commit, err := commitHash(manifest[imp].Dir)
fmt.Fprintf(w, "%s\t%s\n", commit, imp)
if err != nil {
log.Printf("%s: commit hash: %v", imp, err)
}
}
return w.Flush()
}
func vendor(names []string, andDeps bool) {
ps, err := listPackages(names)
if err != nil {
log.Fatalf("error encountered listing packages: %v", err)
}
for _, p := range ps {
if p.Error != nil {
log.Printf("encountered package error: %v", p.Error.Err)
continue
}
if p.Standard {
continue
}
if isVendored(p) {
if !isLocal(p) {
noteExtVendoredDep(p)
}
continue
}
if !isLocal(p) {
if err := copyPackage(p); err != nil {
log.Printf("error copying package %s: %v", p.ImportPath, err)
continue
}
noteManifest(p)
}
if andDeps {
vendor(p.Deps, false)
}
}
}
func isVendored(p *Package) bool {
return strings.Contains(p.ImportPath, "/vendor/")
}
var cwd string
func getwd() string {
if cwd == "" {
var err error
cwd, err = os.Getwd()
if err != nil {
log.Fatalf("error getting current directory: %v", err)
}
}
return cwd
}
func isLocal(d *Package) bool {
return strings.HasPrefix(d.Dir, getwd())
}
// listPackages returns all packages in name
func listPackages(names []string) ([]*Package, error) {
args := append([]string{"list", "-json"}, names...)
cmd := exec.Command("go", args...)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
d := json.NewDecoder(stdout)
var ps []*Package
for {
p := new(Package)
err := d.Decode(p)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
ps = append(ps, p)
}
if err := cmd.Wait(); err != nil {
return nil, err
}
return ps, nil
}
func copyPackage(p *Package) error {
vdir := filepath.Join("vendor", p.ImportPath)
if err := os.MkdirAll(vdir, 0755); err != nil {
return err
}
files := flatten(
p.GoFiles,
p.CgoFiles,
p.IgnoredGoFiles,
p.CFiles,
p.CXXFiles,
p.MFiles,
p.HFiles,
p.SFiles,
p.SwigFiles,
p.SwigCXXFiles,
p.SysoFiles,
)
for _, fname := range files {
if err := copyFile(
filepath.Join(vdir, fname),
filepath.Join(p.Dir, fname),
); err != nil {
return err
}
}
return nil
}
func copyFile(dstpath, srcpath string) error {
dst, err := os.Create(dstpath)
if err != nil {
return err
}
defer dst.Close()
src, err := os.Open(srcpath)
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(dst, src)
return err
}
func commitHash(dir string) (string, error) {
// TODO: work with hg, bzr
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = dir
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
return "unknown", err
}
commit := string(bytes.TrimSpace(out))
if !isClean(dir) {
commit += " (dirty)"
}
return commit, nil
}
func isClean(dir string) bool {
cmd := exec.Command("git", "diff-index", "--quiet", "HEAD")
cmd.Dir = dir
return cmd.Run() == nil
}
func flatten(sss ...[]string) (ss []string) {
for _, v := range sss {
ss = append(ss, v...)
}
return
}