-
Notifications
You must be signed in to change notification settings - Fork 80
/
ecosystem.go
97 lines (89 loc) · 2.59 KB
/
ecosystem.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
package indexer
import (
"context"
"github.com/quay/zlog"
)
// Ecosystems group together scanners and a Coalescer which are commonly used
// together.
//
// A typical ecosystem is "dpkg" which will use the "dpkg" package indexer, the
// "os-release" distribution scanner and the "apt" repository scanner.
//
// A Controller will scan layers with all scanners present in its configured
// ecosystems.
type Ecosystem struct {
PackageScanners func(ctx context.Context) ([]PackageScanner, error)
DistributionScanners func(ctx context.Context) ([]DistributionScanner, error)
RepositoryScanners func(ctx context.Context) ([]RepositoryScanner, error)
FileScanners func(ctx context.Context) ([]FileScanner, error)
Coalescer func(ctx context.Context) (Coalescer, error)
Name string
}
// EcosystemsToScanners extracts and dedupes multiple ecosystems and returns
// their discrete scanners.
func EcosystemsToScanners(ctx context.Context, ecosystems []*Ecosystem) ([]PackageScanner, []DistributionScanner, []RepositoryScanner, []FileScanner, error) {
ctx = zlog.ContextWithValues(ctx, "component", "indexer/EcosystemsToScanners")
ps := []PackageScanner{}
ds := []DistributionScanner{}
rs := []RepositoryScanner{}
fis := []FileScanner{}
seen := struct{ pkg, dist, repo, file map[string]struct{} }{
make(map[string]struct{}),
make(map[string]struct{}),
make(map[string]struct{}),
make(map[string]struct{}),
}
for _, ecosystem := range ecosystems {
pscanners, err := ecosystem.PackageScanners(ctx)
if err != nil {
return nil, nil, nil, nil, err
}
for _, s := range pscanners {
n := s.Name()
if _, ok := seen.pkg[n]; ok {
continue
}
seen.pkg[n] = struct{}{}
ps = append(ps, s)
}
dscanners, err := ecosystem.DistributionScanners(ctx)
if err != nil {
return nil, nil, nil, nil, err
}
for _, s := range dscanners {
n := s.Name()
if _, ok := seen.dist[n]; ok {
continue
}
seen.dist[n] = struct{}{}
ds = append(ds, s)
}
rscanners, err := ecosystem.RepositoryScanners(ctx)
if err != nil {
return nil, nil, nil, nil, err
}
for _, s := range rscanners {
n := s.Name()
if _, ok := seen.repo[n]; ok {
continue
}
seen.repo[n] = struct{}{}
rs = append(rs, s)
}
if ecosystem.FileScanners != nil {
fscanners, err := ecosystem.FileScanners(ctx)
if err != nil {
return nil, nil, nil, nil, err
}
for _, s := range fscanners {
n := s.Name()
if _, ok := seen.file[n]; ok {
continue
}
seen.file[n] = struct{}{}
fis = append(fis, s)
}
}
}
return ps, ds, rs, fis, nil
}