-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendorfile.go
197 lines (175 loc) · 5.18 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
)
// Based on:
// https://github.com/kardianos/vendor-spec/blob/aedbf313488aa9887871048ddcc6f8a70ac02eab/README.md
// (commit from 2015.06.13)
//
// Extended with custom fields.
type VendorFile struct {
// FIXME(mateuszc): add comment
Tool string `json:"tool"`
// Platforms is a list of GOOS & GOARCH pairs which were used when crawling
// import dependencies to build the list of packages.
//
// Platforms is a custom field, specific to the "vendo" tool.
Platforms []Platform `json:"platforms,omitempty"`
// Comment is free text for human use. Example "Revision abc123 introduced
// changes that are not backwards compatible, so leave this as def876."
Comment string `json:"comment,omitempty"`
// Packages represents a collection of vendor packages that have been copied
// locally. Each entry represents a single Go package.
Packages []*VendorPackage `json:"package"`
}
type VendorPackage struct {
// Canonical import path. Example "rsc.io/pdf".
// go get <Canonical> should fetch the remote package.
Canonical string `json:"canonical"`
// Package path relative to the vendor file.
// Examples: "vendor/rsc.io/pdf".
//
// Local should always use forward slashes and must not contain the
// path elements "." or "..".
Local string `json:"local"`
// The revision of the package. This field must be persisted by all
// tools, but not all tools will interpret this field.
// The value of Revision should be a single value that can be used
// to fetch the same or similar revision.
// Examples: "abc104...438ade0", "v1.3.5"
Revision string `json:"revision"`
// RevisionTime is the time the revision was created. The time should be
// parsed and written in the "time.RFC3339" format.
RevisionTime string `json:"revisionTime"`
// Comment is free text for human use.
Comment string `json:"comment,omitempty"`
// RepositoryRoot is the package root repository. You can find repo metadata
// directories here (.git, .hg, etc.)
// Examples: "vendor/rsc.io/pdf".
//
// RepositoryRoot is custom field, specific for "vendo" tool. It must
// always use forward slashes and must not contain the path elements "."
// or "..".
RepositoryRoot string `json:"repositoryRoot"`
}
type Platform struct {
Os string `json:"os"`
Arch string `json:"arch"`
}
func (p Platform) MarshalJSON() ([]byte, error) {
return json.Marshal(p.Os + "_" + p.Arch)
}
func (p *Platform) UnmarshalJSON(data []byte) error {
s := ""
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
split := strings.Split(s, "_")
if len(split) != 2 {
return fmt.Errorf("platform code must have exactly one '_' char in: %s", s)
}
p.Os, p.Arch = split[0], split[1]
return nil
}
func ReadVendorFile(path string) (*VendorFile, error) {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return &VendorFile{}, nil
}
return nil, err
}
defer f.Close()
return ParseVendorFile(f)
}
func ReadStagedVendorFile(path string) (*VendorFile, error) {
data, err := git{}.ReadStaged(".", path)
if err != nil {
if os.IsNotExist(err) {
return &VendorFile{}, nil
}
return nil, err
}
defer data.Close()
return ParseVendorFile(data)
}
func ReadHeadVendorFile(path string) (*VendorFile, error) {
data, err := git{}.ReadHead(".", path)
if err != nil {
if os.IsNotExist(err) {
return &VendorFile{}, nil
}
return nil, err
}
defer data.Close()
return ParseVendorFile(data)
}
func ParseVendorFile(r io.Reader) (*VendorFile, error) {
data := VendorFile{}
err := json.NewDecoder(r).Decode(&data)
if err != nil {
return nil, err
}
return &data, nil
}
func (v *VendorFile) WriteTo(path string) error {
buf, err := json.MarshalIndent(v, "", " ")
if err != nil {
// TODO(mateuszc): add more context to error message?
return err
}
err = ioutil.WriteFile(path, buf, 0644)
if err != nil {
// TODO(mateuszc): add more context to error message?
return err
}
return nil
}
func (v *VendorFile) ByCanonical() map[string]*VendorPackage {
m := map[string]*VendorPackage{}
for _, pkg := range v.Packages {
// FIXME(mateuszc): resolve somehow situation when identical .Canonical fields are repeated
m[pkg.Canonical] = pkg
}
return m
}
func (v *VendorFile) ByRepositoryRoot() map[string]*VendorPackage {
m := map[string]*VendorPackage{}
for _, pkg := range v.Packages {
// FIXME(mateuszc): resolve somehow situation when identical .RepositoryRoot fields are repeated
m[pkg.RepositoryRoot] = pkg
}
return m
}
// NOTE(mateuszc): files and v's "repositoryRoot"s must be relative, slash-only
// paths
func (v *VendorFile) findReposOfFiles(files []string) (set, []string) {
result := set{}
unknown := []string{}
repos := v.ByRepositoryRoot()
nextFile:
for _, file := range files {
dir := file
for dir != "." {
dir = path.Dir(dir)
_, found := repos[dir]
if found {
result.Add(dir)
continue nextFile
}
}
unknown = append(unknown, file)
}
return result, unknown
}
type PackagesOrder []*VendorPackage
func (p PackagesOrder) Len() int { return len(p) }
func (p PackagesOrder) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PackagesOrder) Less(i, j int) bool { return p[i].Canonical < p[j].Canonical }