-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagecache.go
67 lines (55 loc) · 1.47 KB
/
imagecache.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
package main
import (
"fmt"
"os"
"path"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func getImagePathForName(imageName string) (string, error) {
_, err := os.Stat(imageName)
if err == nil {
return imageName, nil
}
if !os.IsNotExist(err) {
return "", fmt.Errorf("problem accessing image %s: %w", imageName, err)
}
containerCachePath, ok := os.LookupEnv("SHARK_CONTAINER_CACHE")
if !ok {
containerCachePath = path.Join(os.Getenv("HOME"), ".shark")
}
err = os.MkdirAll(containerCachePath, os.ModePerm)
if err != nil {
return "", fmt.Errorf("could not find container cache %v: %w", containerCachePath, err)
}
ref, err := name.ParseReference(imageName, name.Insecure)
if err != nil {
return "", err
}
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
panic(err)
}
hash, err := img.Digest()
if err != nil {
panic(err)
}
imageMap := map[string]v1.Image{}
imageMap[imageName] = img
path := path.Join(containerCachePath, fmt.Sprintf("%s.tar", hash.Hex))
_, err = os.Stat(path)
if err == nil {
return path, nil
}
if !os.IsNotExist(err) {
return "", err
}
err = crane.MultiSave(imageMap, path)
if err != nil {
panic(fmt.Errorf("saving tarball %s: %w", path, err))
}
return path, err
}