From 850482b3145db838228215f30ae1736bf07b14eb Mon Sep 17 00:00:00 2001 From: Jake Correnti Date: Tue, 1 Aug 2023 09:53:38 -0400 Subject: [PATCH] Move alternate image acquisition to separate function Moves acquisition of an alternate image provided by the user out of `acquireVMImage` in `pkg/machine//machine.go` and into `pkg/machine/pull.go` as its own function. Signed-off-by: Jake Correnti --- pkg/machine/applehv/machine.go | 9 +-------- pkg/machine/pull.go | 20 ++++++++++++++++++++ pkg/machine/qemu/machine.go | 11 +---------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/machine/applehv/machine.go b/pkg/machine/applehv/machine.go index 48c76df4fa..e018d71d78 100644 --- a/pkg/machine/applehv/machine.go +++ b/pkg/machine/applehv/machine.go @@ -100,18 +100,11 @@ func (m *MacMachine) acquireVMImage(opts machine.InitOptions, dataDir string) er // The user has provided an alternate image which can be a file path // or URL. m.ImageStream = "custom" - g, err := machine.NewGenericDownloader(vmtype, m.Name, opts.ImagePath) - if err != nil { - return err - } - imagePath, err := machine.NewMachineFile(g.Get().LocalUncompressedFile, nil) + imagePath, err := machine.AcquireAlternateImage(m.Name, vmtype, opts) if err != nil { return err } m.ImagePath = *imagePath - if err := machine.DownloadImage(g); err != nil { - return err - } } return nil } diff --git a/pkg/machine/pull.go b/pkg/machine/pull.go index 113cf5da97..16abcdf794 100644 --- a/pkg/machine/pull.go +++ b/pkg/machine/pull.go @@ -385,3 +385,23 @@ func RemoveImageAfterExpire(dir string, expire time.Duration) error { }) return err } + +// AcquireAlternateImage downloads the alternate image the user provided, which +// can be a file path or URL +func AcquireAlternateImage(name string, vmtype VMType, opts InitOptions) (*VMFile, error) { + g, err := NewGenericDownloader(vmtype, name, opts.ImagePath) + if err != nil { + return nil, err + } + + imagePath, err := NewMachineFile(g.Get().LocalUncompressedFile, nil) + if err != nil { + return nil, err + } + + if err := DownloadImage(g); err != nil { + return nil, err + } + + return imagePath, nil +} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index b04ccc7844..699fccfca6 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -225,20 +225,11 @@ func (v *MachineVM) acquireVMImage(opts machine.InitOptions) error { // The user has provided an alternate image which can be a file path // or URL. v.ImageStream = "custom" - g, err := machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath) + imagePath, err := machine.AcquireAlternateImage(v.Name, vmtype, opts) if err != nil { return err } - - imagePath, err := machine.NewMachineFile(g.Get().LocalUncompressedFile, nil) - if err != nil { - return err - } - v.ImagePath = *imagePath - if err := machine.DownloadImage(g); err != nil { - return err - } } return nil }