This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
versions.go
354 lines (279 loc) · 7.23 KB
/
versions.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package versions
import (
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"github.com/go-enry/go-license-detector/v4/licensedb"
"github.com/go-enry/go-license-detector/v4/licensedb/api"
"github.com/go-enry/go-license-detector/v4/licensedb/filer"
"github.com/senseyeio/diligent"
"golang.org/x/mod/modfile"
)
type (
// GoVersion defines the module version of Go used by the Module.
GoVersion string
// ModuleName defines the name of the module.
ModuleName string
// PackageName defines the name of the package.
PackageName string
//-
// License represents the LICENSE used by a Package.
License struct {
Identifier string
Name string
ShortName string
Type diligent.Type
Category diligent.Category
}
// Package represents an imported Go packaged in a Module.
Package struct {
Name PackageName
Version string
IsIndirect bool
ReplacedPath string
ReplacedVersion string
License License
}
//-
// GoVersions handles Go versions used by different Modules.
GoVersions struct {
values map[ModuleName]GoVersion
order []ModuleName
last GoVersion
same bool
}
// Packages handles Packages used by different modules.
Packages struct {
packages map[PackageName]map[ModuleName]Package
lastVersions map[PackageName]Package
sameVersions map[PackageName]bool
names []PackageName
}
//-
// ModuleGoVersion represents a module and its Go version.
ModuleGoVersion struct {
Name ModuleName
GoVersion GoVersion
}
//-
// Module represents the contents of a go.mod file.
Module struct {
ModuleGoVersion
DependencyRequirements map[PackageName]Package
}
// Versions contains the parsed go.mod files.
Versions struct {
Modules map[ModuleName]Module
GoVersions GoVersions
Packages Packages
}
)
// New returns the parsed versions used by all the mod files.
func New(files []string) (Versions, error) {
parsed, err := newModFiles(files)
if err != nil {
return Versions{}, err
}
result := Versions{
Modules: make(map[ModuleName]Module),
}
licenses := make(map[string]License)
for _, modfile := range parsed {
module := newModule(modfile)
result.Modules[module.Name] = module
result.GoVersions.Set(module.Name, module.GoVersion)
for k, pkg := range module.DependencyRequirements {
license, ok := licenses[pkg.Path()]
if !ok {
license = newLicense(pkg.Path())
licenses[pkg.Path()] = license
}
pkg.License = license
module.DependencyRequirements[k] = pkg
result.Packages.Set(module.Name, pkg)
}
}
return result, nil
}
func goModCache() string {
if gomodcache := os.Getenv("GOMODCACHE"); gomodcache != "" {
return gomodcache
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return filepath.Join(gopath, "pkg", "mod")
}
func newLicense(path string) License {
filer, err := filer.FromDirectory(path)
if err != nil {
return License{}
}
licenses, err := licensedb.Detect(filer)
if err != nil {
return License{}
}
var (
match api.Match
name string
)
for k, v := range licenses {
if name == "" || v.Confidence > match.Confidence {
match = v
name = k
}
}
if name == "" {
return License{}
}
license, err := diligent.GetLicenseFromIdentifier(name)
if err != nil {
return License{}
}
return License{
Identifier: license.Identifier,
Name: license.Name,
ShortName: license.ShortName,
Type: license.Type,
Category: license.Category,
}
}
func newModFiles(files []string) ([]*modfile.File, error) {
parsed := make([]*modfile.File, len(files))
for i, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
f, err := modfile.Parse(file, data, nil)
if err != nil {
return nil, err
}
parsed[i] = f
}
return parsed, nil
}
func newModule(modfile *modfile.File) Module {
module := Module{
ModuleGoVersion: ModuleGoVersion{
Name: ModuleName(modfile.Module.Mod.Path),
GoVersion: GoVersion(modfile.Go.Version),
},
}
dependencies := make(map[PackageName]Package)
for _, require := range modfile.Require {
dependencies[PackageName(require.Mod.Path)] = Package{
Name: PackageName(require.Mod.Path),
Version: require.Mod.Version,
IsIndirect: require.Indirect,
}
}
for _, replace := range modfile.Replace {
pkg, ok := dependencies[PackageName(replace.Old.Path)]
if !ok {
continue
}
pkg.ReplacedPath = replace.New.Path
pkg.ReplacedVersion = replace.New.Version
dependencies[PackageName(replace.Old.Path)] = pkg
}
module.DependencyRequirements = dependencies
return module
}
// IsSame returns true when all Modules use the same Go Version.
func (g *GoVersions) IsSame() bool {
return g.same
}
// Set sets the Go Version being used by the defined Module.
func (g *GoVersions) Set(name ModuleName, version GoVersion) {
if g.last == "" {
g.last = version
g.same = true
} else if g.same {
g.same = g.last == version
}
if g.values == nil {
g.values = make(map[ModuleName]GoVersion)
}
if _, ok := g.values[name]; !ok {
// to keep the Set order
g.order = append(g.order, name)
}
g.values[name] = version
}
// Values returns a copy of currently set modules and their Go versions.
func (g *GoVersions) Values() []ModuleGoVersion {
result := make([]ModuleGoVersion, len(g.order))
for i, name := range g.order {
version := g.values[name]
result[i].Name = name
result[i].GoVersion = version
}
return result
}
// Path returns the full filesystem path pointing to the package
func (p Package) Path() string {
version := func(v string) string {
if v == "" {
return ""
}
return fmt.Sprintf("@%s", v)
}
var newVersion string
if p.ReplacedPath != "" {
newVersion = fmt.Sprintf("%s%s", p.ReplacedPath, version(p.ReplacedVersion))
} else {
newVersion = fmt.Sprintf("%s%s", p.Name, version(p.Version))
}
return filepath.Join(goModCache(), newVersion)
}
// IsSame returns true when all Modules use the same Package Version.
func (p *Packages) IsSame(value PackageName) bool {
if p.sameVersions == nil {
return false
}
return p.sameVersions[value]
}
// Names returns a slice of all package names used in total.
func (p *Packages) Names() []PackageName {
if p.names == nil {
return nil
}
res := make([]PackageName, len(p.packages))
copy(res, p.names)
return res
}
// Set sets the Package being used by the defined Module.
func (p *Packages) Set(name ModuleName, pkg Package) {
if p.packages == nil {
p.packages = make(map[PackageName]map[ModuleName]Package)
p.lastVersions = make(map[PackageName]Package)
p.sameVersions = make(map[PackageName]bool)
}
mods, ok := p.packages[pkg.Name]
if !ok {
mods = make(map[ModuleName]Package)
p.lastVersions[pkg.Name] = pkg
p.sameVersions[pkg.Name] = true
p.names = append(p.names, pkg.Name)
}
mods[name] = pkg
p.packages[pkg.Name] = mods
if p.sameVersions[pkg.Name] && pkg != p.lastVersions[pkg.Name] {
p.sameVersions[pkg.Name] = false
}
}
// Values returns a copy of currently set modules and their packages by package.
func (p *Packages) Values(value PackageName) map[ModuleName]Package {
result := make(map[ModuleName]Package)
if p.packages == nil {
return result
}
for k, v := range p.packages[value] {
result[k] = v
}
return result
}