forked from paketo-buildpacks/occam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpack_store.go
127 lines (105 loc) · 3.13 KB
/
buildpack_store.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package occam
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ForestEckhardt/freezer"
"github.com/ForestEckhardt/freezer/github"
"github.com/paketo-buildpacks/occam/packagers"
)
//go:generate faux --interface LocalFetcher --output fakes/local_fetcher.go
type LocalFetcher interface {
WithPackager(packager freezer.Packager) freezer.LocalFetcher
Get(freezer.LocalBuildpack) (string, error)
}
//go:generate faux --interface RemoteFetcher --output fakes/remote_fetcher.go
type RemoteFetcher interface {
WithPackager(packager freezer.Packager) freezer.RemoteFetcher
Get(freezer.RemoteBuildpack) (string, error)
}
//go:generate faux --interface CacheManager --output fakes/cache_manager.go
type CacheManager interface {
Get(key string) (freezer.CacheEntry, bool, error)
Set(key string, cachedEntry freezer.CacheEntry) error
Dir() string
Open() error
Close() error
}
type BuildpackStore struct {
Get BuildpackStoreGet
}
func NewBuildpackStore() BuildpackStore {
gitToken := os.Getenv("GIT_TOKEN")
cacheManager := freezer.NewCacheManager(filepath.Join(os.Getenv("HOME"), ".freezer-cache"))
releaseService := github.NewReleaseService(github.NewConfig("https://api.github.com", gitToken))
packager := packagers.NewJam()
namer := freezer.NewNameGenerator()
return BuildpackStore{
Get: BuildpackStoreGet{
local: freezer.NewLocalFetcher(
&cacheManager,
packager,
namer,
),
remote: freezer.NewRemoteFetcher(
&cacheManager,
releaseService, packager,
),
cacheManager: &cacheManager,
},
}
}
func (bs BuildpackStore) WithLocalFetcher(fetcher LocalFetcher) BuildpackStore {
bs.Get.local = fetcher
return bs
}
func (bs BuildpackStore) WithRemoteFetcher(fetcher RemoteFetcher) BuildpackStore {
bs.Get.remote = fetcher
return bs
}
func (bs BuildpackStore) WithCacheManager(manager CacheManager) BuildpackStore {
bs.Get.cacheManager = manager
return bs
}
func (bs BuildpackStore) WithPackager(packager freezer.Packager) BuildpackStore {
bs.Get.local = bs.Get.local.WithPackager(packager)
bs.Get.remote = bs.Get.remote.WithPackager(packager)
return bs
}
type BuildpackStoreGet struct {
cacheManager CacheManager
local LocalFetcher
remote RemoteFetcher
offline bool
version string
}
func (g BuildpackStoreGet) Execute(url string) (string, error) {
err := g.cacheManager.Open()
if err != nil {
return "", fmt.Errorf("failed to open cacheManager: %s", err)
}
defer g.cacheManager.Close()
if strings.HasPrefix(url, "github.com") {
request := strings.SplitN(url, "/", 3)
if len(request) < 3 {
return "", fmt.Errorf("error incomplete github.com url: %q", url)
}
buildpack := freezer.NewRemoteBuildpack(request[1], request[2])
buildpack.Offline = g.offline
buildpack.Version = g.version
return g.remote.Get(buildpack)
}
buildpack := freezer.NewLocalBuildpack(url, filepath.Base(url))
buildpack.Offline = g.offline
buildpack.Version = g.version
return g.local.Get(buildpack)
}
func (g BuildpackStoreGet) WithOfflineDependencies() BuildpackStoreGet {
g.offline = true
return g
}
func (g BuildpackStoreGet) WithVersion(version string) BuildpackStoreGet {
g.version = version
return g
}