-
Notifications
You must be signed in to change notification settings - Fork 5
/
remote_fetcher.go
149 lines (123 loc) · 3.56 KB
/
remote_fetcher.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package freezer
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/ForestEckhardt/freezer/github"
"github.com/paketo-buildpacks/packit/v2/vacation"
)
//go:generate faux --interface GitReleaseFetcher --output fakes/git_release_fetcher.go
type GitReleaseFetcher interface {
Get(org, repo string) (github.Release, error)
GetReleaseAsset(asset github.ReleaseAsset) (io.ReadCloser, error)
GetReleaseTarball(url string) (io.ReadCloser, error)
}
//go:generate faux --interface Packager --output fakes/packager.go
type Packager interface {
Execute(buildpackDir, output, version string, cached bool) error
}
//go:generate faux --interface BuildpackCache --output fakes/buildpack_cache.go
type BuildpackCache interface {
Get(key string) (CacheEntry, bool, error)
Set(key string, cachedEntry CacheEntry) error
Dir() string
}
type RemoteFetcher struct {
buildpackCache BuildpackCache
gitReleaseFetcher GitReleaseFetcher
packager Packager
fileSystem func(dir string, pattern string) (string, error)
}
func NewRemoteFetcher(buildpackCache BuildpackCache, gitReleaseFetcher GitReleaseFetcher, packager Packager) RemoteFetcher {
return RemoteFetcher{
buildpackCache: buildpackCache,
gitReleaseFetcher: gitReleaseFetcher,
packager: packager,
fileSystem: os.MkdirTemp,
}
}
func (r RemoteFetcher) WithPackager(packager Packager) RemoteFetcher {
r.packager = packager
return r
}
func (r RemoteFetcher) WithFileSystem(fileSystem func(string, string) (string, error)) RemoteFetcher {
r.fileSystem = fileSystem
return r
}
func (r RemoteFetcher) Get(buildpack RemoteBuildpack) (string, error) {
release, err := r.gitReleaseFetcher.Get(buildpack.Org, buildpack.Repo)
if err != nil {
return "", err
}
buildpackCacheDir := filepath.Join(r.buildpackCache.Dir(), buildpack.Org, buildpack.Repo)
if buildpack.Offline {
buildpackCacheDir = filepath.Join(buildpackCacheDir, "cached")
}
key := buildpack.UncachedKey
if buildpack.Offline {
key = buildpack.CachedKey
}
cachedEntry, exist, err := r.buildpackCache.Get(key)
if err != nil {
return "", err
}
if !exist {
err = os.MkdirAll(buildpackCacheDir, os.ModePerm)
if err != nil {
return "", err
}
}
path := cachedEntry.URI
tagName := strings.TrimPrefix(release.TagName, "v")
if tagName != cachedEntry.Version || !exist {
missingReleaseArtifacts := !(len(release.Assets) > 0)
var bundle io.ReadCloser
if missingReleaseArtifacts || buildpack.Offline {
bundle, err = r.gitReleaseFetcher.GetReleaseTarball(release.TarballURL)
if err != nil {
return "", err
}
} else {
bundle, err = r.gitReleaseFetcher.GetReleaseAsset(release.Assets[0])
if err != nil {
return "", err
}
}
path = filepath.Join(buildpackCacheDir, fmt.Sprintf("%s.cnb", tagName))
if missingReleaseArtifacts || buildpack.Offline {
downloadDir, err := r.fileSystem("", buildpack.Repo)
if err != nil {
return "", err
}
defer os.RemoveAll(downloadDir)
err = vacation.NewArchive(bundle).StripComponents(1).Decompress(downloadDir)
if err != nil {
return "", err
}
err = r.packager.Execute(downloadDir, path, tagName, buildpack.Offline)
if err != nil {
return "", err
}
} else {
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return "", err
}
defer file.Close()
_, err = io.Copy(file, bundle)
if err != nil {
return "", err
}
}
err = r.buildpackCache.Set(key, CacheEntry{
Version: tagName,
URI: path,
})
if err != nil {
return "", err
}
}
return path, nil
}