forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendorFile.go
79 lines (66 loc) · 1.65 KB
/
vendorFile.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"bytes"
ros "os"
"path/filepath"
"strings"
"github.com/dchest/safefile"
"github.com/kardianos/govendor/vendorfile"
os "github.com/kardianos/govendor/internal/vos"
)
// WriteVendorFile writes the current vendor file to the context location.
func (ctx *Context) WriteVendorFile() (err error) {
perm := ros.FileMode(0666)
fi, err := os.Stat(ctx.VendorFilePath)
if err == nil {
perm = fi.Mode()
}
ctx.VendorFile.RootPath = ctx.RootImportPath
buf := &bytes.Buffer{}
err = ctx.VendorFile.Marshal(buf)
if err != nil {
return
}
err = buf.WriteByte('\n')
if err != nil {
return
}
dir, _ := filepath.Split(ctx.VendorFilePath)
err = os.MkdirAll(dir, 0777)
if err != nil {
return
}
for i := range ctx.VendorFile.Package {
vp := ctx.VendorFile.Package[i]
vp.Add = false
}
err = safefile.WriteFile(ctx.VendorFilePath, buf.Bytes(), perm)
if err == nil {
for _, vp := range ctx.VendorFile.Package {
vp.Add = false
}
}
return
}
func readVendorFile(vendorRoot, vendorFilePath string) (*vendorfile.File, error) {
vf := &vendorfile.File{}
f, err := os.Open(vendorFilePath)
if err != nil {
return nil, err
}
defer f.Close()
err = vf.Unmarshal(f)
if err != nil {
return nil, err
}
// Remove any existing origin field if the prefix matches the
// context package root. This fixes a previous bug introduced in the file,
// that is now fixed.
for _, row := range vf.Package {
row.Origin = strings.TrimPrefix(row.Origin, vendorRoot)
}
return vf, nil
}