forked from kardianos/govendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendorFile.go
57 lines (49 loc) · 1.15 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
// 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"
"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()
}
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
}
err = safefile.WriteFile(ctx.VendorFilePath, buf.Bytes(), perm)
return
}
func readVendorFile(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
}
return vf, nil
}