-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
102 lines (91 loc) · 2.33 KB
/
builder.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
package libyear
import (
"path/filepath"
"time"
"github.com/nieomylnieja/go-libyear/internal"
)
func NewCommandBuilder(source Source, output Output) CommandBuilder {
return CommandBuilder{
source: source,
output: output,
}
}
type CommandBuilder struct {
source Source
output Output
repo ModulesRepo
fallback VersionsGetter
withCache bool
cacheFilePath string
opts Option
vcsRegistry *VCSRegistry
ageLimit time.Time
}
func (b CommandBuilder) WithCache(cacheFilePath string) CommandBuilder {
b.withCache = true
b.cacheFilePath = cacheFilePath
return b
}
func (b CommandBuilder) WithModulesRepo(repo ModulesRepo) CommandBuilder {
b.repo = repo
return b
}
func (b CommandBuilder) WithFallbackVersionsGetter(getter VersionsGetter) CommandBuilder {
b.fallback = getter
return b
}
func (b CommandBuilder) WithOptions(opts ...Option) CommandBuilder {
for _, opt := range opts {
b.opts |= opt
}
return b
}
func (b CommandBuilder) WithVCSRegistry(registry *VCSRegistry) CommandBuilder {
b.vcsRegistry = registry
return b
}
func (b CommandBuilder) WithAgeLimit(limit time.Time) CommandBuilder {
b.ageLimit = limit
return b
}
func (b CommandBuilder) Build() (*Command, error) {
if b.repo == nil {
var err error
if b.opts&OptionUseGoList != 0 {
b.repo, err = internal.NewGoListExecutor(b.withCache, b.cacheFilePath)
} else {
b.repo, err = internal.NewGoProxyClient(b.withCache, b.cacheFilePath)
}
if err != nil {
return nil, err
}
}
if b.fallback == nil {
b.fallback = internal.NewDepsDevClient()
}
// Share initialized ModulesRepo with sources.
if v, ok := b.source.(interface{ SetModulesRepo(repo ModulesRepo) }); ok {
v.SetModulesRepo(b.repo)
}
if b.vcsRegistry == nil {
cacheBase, err := internal.GetDefaultCacheBasePath()
if err != nil {
return nil, err
}
cacheDir := filepath.Join(cacheBase, "vcs")
b.vcsRegistry = NewVCSRegistry(cacheDir)
}
// Share initialized VCSRegistry with sources.
if v, ok := b.source.(interface{ SetVCSRegistry(registry *VCSRegistry) }); ok {
v.SetVCSRegistry(b.vcsRegistry)
}
return &Command{
source: b.source,
output: b.output,
repo: b.repo,
fallbackVersions: b.fallback,
opts: b.opts,
vcs: b.vcsRegistry,
ageLimit: b.ageLimit,
}, nil
}