| 1 | |
| 2 | |
| 3 | |
| 4 | package cacheutil |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "path" |
| 10 | |
| 11 | "github.com/lima-vm/lima/v2/pkg/downloader" |
| 12 | "github.com/lima-vm/lima/v2/pkg/fileutils" |
| 13 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | func NerdctlArchive(y *limatype.LimaYAML) string { |
| 18 | if *y.Containerd.System || *y.Containerd.User { |
| 19 | for _, f := range y.Containerd.Archives { |
| 20 | if f.Arch == *y.Arch { |
| 21 | return path.Base(f.Location) |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | return "" |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | func EnsureNerdctlArchiveCache(ctx context.Context, y *limatype.LimaYAML, created bool) (string, error) { |
| 32 | if !*y.Containerd.System && !*y.Containerd.User { |
| 33 | |
| 34 | return "", nil |
| 35 | } |
| 36 | |
| 37 | errs := make([]error, len(y.Containerd.Archives)) |
| 38 | for i, f := range y.Containerd.Archives { |
| 39 | |
| 40 | if created && f.Arch == *y.Arch && !downloader.IsLocal(f.Location) { |
| 41 | path, err := fileutils.CachedFile(f) |
| 42 | if err == nil { |
| 43 | return path, nil |
| 44 | } |
| 45 | } |
| 46 | path, err := fileutils.DownloadFile(ctx, "", f, false, "the nerdctl archive", *y.Arch, nil) |
| 47 | if err != nil { |
| 48 | errs[i] = err |
| 49 | continue |
| 50 | } |
| 51 | if path == "" { |
| 52 | if downloader.IsLocal(f.Location) { |
| 53 | return f.Location, nil |
| 54 | } |
| 55 | return "", fmt.Errorf("cache did not contain %#q", f.Location) |
| 56 | } |
| 57 | return path, nil |
| 58 | } |
| 59 | |
| 60 | return "", fileutils.Errors(errs) |
| 61 | } |