-
Notifications
You must be signed in to change notification settings - Fork 0
/
registryprovider.go
72 lines (64 loc) · 1.55 KB
/
registryprovider.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
package mappings
import (
"encoding/json"
"io"
"net/http"
"regexp"
)
const REGISTRY_URL = "https://raw.githubusercontent.com/CREDOProject/package-suggestions/main/registry.json"
type Matcher struct {
Matcher string `json:"matcher"`
Packages []Dependency `json:"packages"`
}
// Describes a Dependency to be parsed in the client application.
type Dependency struct {
Name string `json:"name"`
PackageManager string `json:"manager"`
}
// registryMappingProvider implements MappingProvider.
type registryMappingProvider struct {
Mappings map[*regexp.Regexp][]Dependency
}
func NewRegistryMappingProvider() *registryMappingProvider {
return ®istryMappingProvider{}
}
func (r *registryMappingProvider) Get(name string) (deps []Dependency) {
err := r.retrieveMappings()
if err != nil {
return nil
}
for regex, dependencies := range r.Mappings {
if ok := regex.MatchString(name); ok {
deps = append(deps, dependencies...)
}
}
return
}
func (r *registryMappingProvider) retrieveMappings() (err error) {
if len(r.Mappings) > 0 {
return
}
r.Mappings = make(map[*regexp.Regexp][]Dependency)
response, err := http.Get(REGISTRY_URL)
if err != nil {
return
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return
}
proxyData := []Matcher{}
err = json.Unmarshal(body, &proxyData)
if err != nil {
return
}
for _, p := range proxyData {
regex, err := regexp.Compile(p.Matcher)
if err != nil {
return err
}
r.Mappings[regex] = append(r.Mappings[regex], p.Packages...)
}
return
}