Move alternate image acquisition to separate function

Moves acquisition of an alternate image provided by the user out of
`acquireVMImage` in `pkg/machine/<hypervisor>/machine.go` and into
`pkg/machine/pull.go` as its own function.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti
2023-08-01 09:53:38 -04:00
parent 906af5bbc6
commit 850482b314
3 changed files with 22 additions and 18 deletions

View File

@ -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 // The user has provided an alternate image which can be a file path
// or URL. // or URL.
m.ImageStream = "custom" m.ImageStream = "custom"
g, err := machine.NewGenericDownloader(vmtype, m.Name, opts.ImagePath) imagePath, err := machine.AcquireAlternateImage(m.Name, vmtype, opts)
if err != nil {
return err
}
imagePath, err := machine.NewMachineFile(g.Get().LocalUncompressedFile, nil)
if err != nil { if err != nil {
return err return err
} }
m.ImagePath = *imagePath m.ImagePath = *imagePath
if err := machine.DownloadImage(g); err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -385,3 +385,23 @@ func RemoveImageAfterExpire(dir string, expire time.Duration) error {
}) })
return err 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
}

View File

@ -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 // The user has provided an alternate image which can be a file path
// or URL. // or URL.
v.ImageStream = "custom" v.ImageStream = "custom"
g, err := machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath) imagePath, err := machine.AcquireAlternateImage(v.Name, vmtype, opts)
if err != nil { if err != nil {
return err return err
} }
imagePath, err := machine.NewMachineFile(g.Get().LocalUncompressedFile, nil)
if err != nil {
return err
}
v.ImagePath = *imagePath v.ImagePath = *imagePath
if err := machine.DownloadImage(g); err != nil {
return err
}
} }
return nil return nil
} }