Skip to content

Commit 4d5bca5

Browse files
committed
Refactor: use fileutils for the nerdctl archive
Don't duplicate the code, but use fileutils.DownloadFile for all. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 5a9bca3 commit 4d5bca5

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

pkg/fileutils/download.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
"github.com/sirupsen/logrus"
99
)
1010

11-
func DownloadFile(dest string, f limayaml.File, description string, expectedArch limayaml.Arch) error {
11+
// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
12+
func DownloadFile(dest string, f limayaml.File, description string, expectedArch limayaml.Arch) (string, error) {
1213
if f.Arch != expectedArch {
13-
return fmt.Errorf("unsupported arch: %q", f.Arch)
14+
return "", fmt.Errorf("unsupported arch: %q", f.Arch)
1415
}
1516
logrus.WithField("digest", f.Digest).Infof("Attempting to download %s from %q", description, f.Location)
1617
res, err := downloader.Download(dest, f.Location,
1718
downloader.WithCache(),
1819
downloader.WithExpectedDigest(f.Digest),
1920
)
2021
if err != nil {
21-
return fmt.Errorf("failed to download %q: %w", f.Location, err)
22+
return "", fmt.Errorf("failed to download %q: %w", f.Location, err)
2223
}
2324
logrus.Debugf("res.ValidatedDigest=%v", res.ValidatedDigest)
2425
switch res.Status {
@@ -29,5 +30,5 @@ func DownloadFile(dest string, f limayaml.File, description string, expectedArch
2930
default:
3031
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
3132
}
32-
return nil
33+
return res.CachePath, nil
3334
}

pkg/qemu/qemu.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ func EnsureDisk(cfg Config) error {
5050
var ensuredBaseDisk bool
5151
errs := make([]error, len(cfg.LimaYAML.Images))
5252
for i, f := range cfg.LimaYAML.Images {
53-
if err := fileutils.DownloadFile(baseDisk, f.File, "the image", *cfg.LimaYAML.Arch); err != nil {
53+
if _, err := fileutils.DownloadFile(baseDisk, f.File, "the image", *cfg.LimaYAML.Arch); err != nil {
5454
errs[i] = err
5555
continue
5656
}
5757
if f.Kernel != nil {
58-
if err := fileutils.DownloadFile(kernel, f.Kernel.File, "the kernel", *cfg.LimaYAML.Arch); err != nil {
58+
if _, err := fileutils.DownloadFile(kernel, f.Kernel.File, "the kernel", *cfg.LimaYAML.Arch); err != nil {
5959
errs[i] = err
6060
continue
6161
}
@@ -67,7 +67,7 @@ func EnsureDisk(cfg Config) error {
6767
}
6868
}
6969
if f.Initrd != nil {
70-
if err := fileutils.DownloadFile(initrd, *f.Initrd, "the initrd", *cfg.LimaYAML.Arch); err != nil {
70+
if _, err := fileutils.DownloadFile(initrd, *f.Initrd, "the initrd", *cfg.LimaYAML.Arch); err != nil {
7171
errs[i] = err
7272
continue
7373
}

pkg/start/start.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lima-vm/lima/pkg/driverutil"
1717

1818
"github.com/lima-vm/lima/pkg/downloader"
19+
"github.com/lima-vm/lima/pkg/fileutils"
1920
hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
2021
"github.com/lima-vm/lima/pkg/limayaml"
2122
"github.com/lima-vm/lima/pkg/store"
@@ -37,33 +38,19 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
3738
}
3839

3940
errs := make([]error, len(y.Containerd.Archives))
40-
for i := range y.Containerd.Archives {
41-
f := &y.Containerd.Archives[i]
42-
if f.Arch != *y.Arch {
43-
errs[i] = fmt.Errorf("unsupported arch: %q", f.Arch)
44-
continue
45-
}
46-
logrus.WithField("digest", f.Digest).Infof("Attempting to download the nerdctl archive from %q", f.Location)
47-
res, err := downloader.Download("", f.Location, downloader.WithCache(), downloader.WithExpectedDigest(f.Digest))
41+
for i, f := range y.Containerd.Archives {
42+
path, err := fileutils.DownloadFile("", f, "the nerdctl archive", *y.Arch)
4843
if err != nil {
49-
errs[i] = fmt.Errorf("failed to download %q: %w", f.Location, err)
44+
errs[i] = err
5045
continue
5146
}
52-
switch res.Status {
53-
case downloader.StatusDownloaded:
54-
logrus.Infof("Downloaded the nerdctl archive from %q", f.Location)
55-
case downloader.StatusUsedCache:
56-
logrus.Infof("Using cache %q", res.CachePath)
57-
default:
58-
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
59-
}
60-
if res.CachePath == "" {
47+
if path == "" {
6148
if downloader.IsLocal(f.Location) {
6249
return f.Location, nil
6350
}
6451
return "", fmt.Errorf("cache did not contain %q", f.Location)
6552
}
66-
return res.CachePath, nil
53+
return path, nil
6754
}
6855

6956
return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v",

pkg/vz/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func EnsureDisk(driver *driver.BaseDriver) error {
2929
var ensuredBaseDisk bool
3030
errs := make([]error, len(driver.Yaml.Images))
3131
for i, f := range driver.Yaml.Images {
32-
if err := fileutils.DownloadFile(baseDisk, f.File, "the image", *driver.Yaml.Arch); err != nil {
32+
if _, err := fileutils.DownloadFile(baseDisk, f.File, "the image", *driver.Yaml.Arch); err != nil {
3333
errs[i] = err
3434
continue
3535
}

0 commit comments

Comments
 (0)