-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcs.go
72 lines (62 loc) · 1.76 KB
/
vcs.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 libyear
import (
"os"
"strings"
"github.com/pkg/errors"
"golang.org/x/mod/module"
"github.com/nieomylnieja/go-libyear/internal"
)
//go:generate mockgen -destination internal/mocks/vcs.go -package mocks -typed . VCSHandler
// VCSHandler is an interface that can be implemented by specifc VCS handler.
type VCSHandler interface {
ModulesRepo
// CanHandle reports whether the vcs can handle the given path.
CanHandle(path string) (bool, error)
// Name reports the name of the VCS system.
Name() string
}
func NewVCSRegistry(cacheDir string) *VCSRegistry {
return &VCSRegistry{
vcsHandlers: []VCSHandler{
internal.NewGitVCS(cacheDir, internal.GitCmd{}),
},
goprivate: os.Getenv("GOPRIVATE"),
}
}
// VCSRegistry implements [command.ModulesRepo] and delegates handling of an
// invoked method to the registered VCS handler which supports the given path.
type VCSRegistry struct {
vcsHandlers []VCSHandler
goprivate string
}
func (v *VCSRegistry) IsPrivate(path string) bool {
return module.MatchPrefixPatterns(v.goprivate, path)
}
// GetHandler returns the VCS handler which supports the given path.
// nolint: ireturn
func (v *VCSRegistry) GetHandler(path string) (ModulesRepo, error) {
var handler VCSHandler
for _, h := range v.vcsHandlers {
canHandle, err := h.CanHandle(path)
if err != nil {
return nil, err
}
if canHandle {
handler = h
break
}
}
if handler == nil {
return nil, errors.Errorf(
"private module path: '%s' cannot be handled by any supported VCS [%s]",
path, v.supportedVCS())
}
return handler, nil
}
func (v *VCSRegistry) supportedVCS() string {
strs := make([]string, 0, len(v.vcsHandlers))
for _, handler := range v.vcsHandlers {
strs = append(strs, handler.Name())
}
return strings.Join(strs, ", ")
}