-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_fetcher.go
84 lines (69 loc) · 1.75 KB
/
local_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
package freezer
import (
"fmt"
"os"
"path/filepath"
)
//go:generate faux --interface Namer --output fakes/namer.go
type Namer interface {
RandomName(name string) (string, error)
}
type LocalFetcher struct {
buildpackCache BuildpackCache
packager Packager
namer Namer
}
func NewLocalFetcher(buildpackCache BuildpackCache, packager Packager, namer Namer) LocalFetcher {
return LocalFetcher{
buildpackCache: buildpackCache,
packager: packager,
namer: namer,
}
}
func (l LocalFetcher) WithPackager(packager Packager) LocalFetcher {
l.packager = packager
return l
}
func (l LocalFetcher) Get(buildpack LocalBuildpack) (string, error) {
buildpackCacheDir := filepath.Join(l.buildpackCache.Dir(), buildpack.Name)
if buildpack.Offline {
buildpackCacheDir = filepath.Join(buildpackCacheDir, "cached")
}
key := buildpack.UncachedKey
if buildpack.Offline {
key = buildpack.CachedKey
}
name, err := l.namer.RandomName(buildpack.Name)
if err != nil {
return "", fmt.Errorf("random name generation failed: %w", err)
}
path := filepath.Join(buildpackCacheDir, fmt.Sprintf("%s.cnb", name))
cachedEntry, exist, err := l.buildpackCache.Get(key)
if err != nil {
return "", err
}
if !exist {
err := os.MkdirAll(buildpackCacheDir, os.ModePerm)
if err != nil {
return "", err
}
} else {
//Add locking logic or override logic
err := os.RemoveAll(cachedEntry.URI)
if err != nil {
return "", err
}
}
err = l.packager.Execute(buildpack.Path, path, buildpack.Version, buildpack.Offline)
if err != nil {
return "", fmt.Errorf("failed to package buildpack: %w", err)
}
err = l.buildpackCache.Set(key, CacheEntry{
Version: "testing",
URI: path,
})
if err != nil {
return "", err
}
return path, nil
}