This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
registry.go
69 lines (61 loc) · 2.29 KB
/
registry.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
package registry
import (
"errors"
"fmt"
"github.com/bazelbuild/bzlmod/common"
"github.com/bazelbuild/bzlmod/fetch"
urls "net/url"
)
// Registry represents a Bazel module registry.
type Registry interface {
// URL returns the URL uniquely identifying the registry.
URL() string
// GetModuleBazel retrieves the MODULE.bazel file of the module with the given key. Returns an error wrapping
// ErrNotFound if no such module exists in the registry.
GetModuleBazel(key common.ModuleKey) ([]byte, error)
// GetFetcher returns the Fetcher object which can be used to fetch the module with the given key. Returns an error
// wrapping ErrNotFound if no such module exists in the registry.
GetFetcher(key common.ModuleKey) (fetch.Fetcher, error)
}
var schemes = make(map[string]func(url *urls.URL) (Registry, error))
// New creates a new Registry object from its URL. The scheme of the URL determines the type of the registry.
func New(rawurl string) (Registry, error) {
url, err := urls.Parse(rawurl)
if err != nil {
return nil, err
}
fn := schemes[url.Scheme]
if fn == nil {
return nil, fmt.Errorf("unrecognized registry scheme %v", url.Scheme)
}
return fn(url)
}
var ErrNotFound = errors.New("module not found")
// GetModuleBazel gets the MODULE.bazel file contents for the module with the given key, using the list of
// registries with an optional override `regOverride` (use an empty string for no override).
// Returns the file contents, and the registry that actually has that module.
func GetModuleBazel(key common.ModuleKey, registries []string, regOverride string) ([]byte, Registry, error) {
if regOverride != "" {
reg, err := New(regOverride)
if err != nil {
return nil, nil, fmt.Errorf("error creating override registry: %v", err)
}
moduleBazel, err := reg.GetModuleBazel(key)
return moduleBazel, reg, err
}
for _, url := range registries {
reg, err := New(url)
if err != nil {
return nil, nil, fmt.Errorf("error creating registry from %q: %v", url, err)
}
moduleBazel, err := reg.GetModuleBazel(key)
if errors.Is(err, ErrNotFound) {
continue
} else if err != nil {
return nil, reg, err
}
return moduleBazel, reg, err
}
// The module couldn't be found in any of the registries.
return nil, nil, fmt.Errorf("%w: %v in registries %q", ErrNotFound, key, registries)
}