From 7aef3301da43fcb7d1dee61b11667b84be59e83a Mon Sep 17 00:00:00 2001
From: Brent Baude <bbaude@redhat.com>
Date: Mon, 23 Jan 2023 10:33:44 -0600
Subject: [PATCH] WSL refactoring

Small amount of refactoring to make WSL specific stuff into the WSL
package where possible.  This is in preparation for the possibility of
adding more virtualization backends.

[NO NEW TESTS NEEDED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
---
 pkg/machine/fcos.go             | 12 +++++------
 pkg/machine/fedora_unix.go      |  2 +-
 pkg/machine/fedora_windows.go   |  2 +-
 pkg/machine/pull.go             |  4 ++--
 pkg/machine/{ => wsl}/fedora.go | 35 +++++++++++++++++----------------
 pkg/machine/wsl/machine.go      |  2 +-
 6 files changed, 29 insertions(+), 28 deletions(-)
 rename pkg/machine/{ => wsl}/fedora.go (83%)

diff --git a/pkg/machine/fcos.go b/pkg/machine/fcos.go
index 47caa4dc63..16f845a786 100644
--- a/pkg/machine/fcos.go
+++ b/pkg/machine/fcos.go
@@ -61,7 +61,7 @@ func NewFcosDownloader(vmType, vmName, imageStream string) (DistributionDownload
 
 	fcd := FcosDownload{
 		Download: Download{
-			Arch:      getFcosArch(),
+			Arch:      GetFcosArch(),
 			Artifact:  artifact,
 			CacheDir:  cacheDir,
 			Format:    Format,
@@ -76,7 +76,7 @@ func NewFcosDownloader(vmType, vmName, imageStream string) (DistributionDownload
 	if err != nil {
 		return nil, err
 	}
-	fcd.Download.LocalUncompressedFile = fcd.getLocalUncompressedFile(dataDir)
+	fcd.Download.LocalUncompressedFile = fcd.GetLocalUncompressedFile(dataDir)
 	return fcd, nil
 }
 
@@ -118,10 +118,10 @@ func (f FcosDownload) CleanCache() error {
 	// Set cached image to expire after 2 weeks
 	// FCOS refreshes around every 2 weeks, assume old images aren't needed
 	expire := 14 * 24 * time.Hour
-	return removeImageAfterExpire(f.CacheDir, expire)
+	return RemoveImageAfterExpire(f.CacheDir, expire)
 }
 
-func getFcosArch() string {
+func GetFcosArch() string {
 	var arch string
 	// TODO fill in more architectures
 	switch runtime.GOARCH {
@@ -189,7 +189,7 @@ func GetFCOSDownload(imageStream string) (*FcosDownloadInfo, error) {
 			return nil, err
 		}
 
-		arches, ok := altMeta.Architectures[getFcosArch()]
+		arches, ok := altMeta.Architectures[GetFcosArch()]
 		if !ok {
 			return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
 		}
@@ -209,7 +209,7 @@ func GetFCOSDownload(imageStream string) (*FcosDownloadInfo, error) {
 	if err := json.Unmarshal(body, &fcosstable); err != nil {
 		return nil, err
 	}
-	arch, ok := fcosstable.Architectures[getFcosArch()]
+	arch, ok := fcosstable.Architectures[GetFcosArch()]
 	if !ok {
 		return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
 	}
diff --git a/pkg/machine/fedora_unix.go b/pkg/machine/fedora_unix.go
index 0fa1d1ad9f..fded150094 100644
--- a/pkg/machine/fedora_unix.go
+++ b/pkg/machine/fedora_unix.go
@@ -7,6 +7,6 @@ import (
 	"runtime"
 )
 
-func determineFedoraArch() string {
+func DetermineMachineArch() string {
 	return runtime.GOARCH
 }
diff --git a/pkg/machine/fedora_windows.go b/pkg/machine/fedora_windows.go
index ff25520e0c..a3cd7f0328 100644
--- a/pkg/machine/fedora_windows.go
+++ b/pkg/machine/fedora_windows.go
@@ -8,7 +8,7 @@ import (
 	"golang.org/x/sys/windows"
 )
 
-func determineFedoraArch() string {
+func DetermineMachineArch() string {
 	const fallbackMsg = "this may result in the wrong Linux arch under emulation"
 	var machine, native uint16
 	current, _ := syscall.GetCurrentProcess()
diff --git a/pkg/machine/pull.go b/pkg/machine/pull.go
index 8b5f30f7c7..fd707319be 100644
--- a/pkg/machine/pull.go
+++ b/pkg/machine/pull.go
@@ -68,7 +68,7 @@ func NewGenericDownloader(vmType, vmName, pullPath string) (DistributionDownload
 	return gd, nil
 }
 
-func (d Download) getLocalUncompressedFile(dataDir string) string {
+func (d Download) GetLocalUncompressedFile(dataDir string) string {
 	var (
 		extension string
 	)
@@ -273,7 +273,7 @@ func decompressEverythingElse(src string, output io.WriteCloser) error {
 	return err
 }
 
-func removeImageAfterExpire(dir string, expire time.Duration) error {
+func RemoveImageAfterExpire(dir string, expire time.Duration) error {
 	now := time.Now()
 	err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
 		// Delete any cache files that are older than expiry date
diff --git a/pkg/machine/fedora.go b/pkg/machine/wsl/fedora.go
similarity index 83%
rename from pkg/machine/fedora.go
rename to pkg/machine/wsl/fedora.go
index cf9bbb793d..8f97e505da 100644
--- a/pkg/machine/fedora.go
+++ b/pkg/machine/wsl/fedora.go
@@ -1,20 +1,21 @@
 //go:build amd64 || arm64
 // +build amd64 arm64
 
-package machine
+package wsl
 
 import (
 	"errors"
 	"fmt"
 	"io"
-	"os"
-	"path"
-	"strings"
-
 	"net/http"
 	"net/url"
+	"os"
+	"path"
 	"path/filepath"
+	"strings"
 	"time"
+
+	"github.com/containers/podman/v4/pkg/machine"
 )
 
 const (
@@ -23,16 +24,16 @@ const (
 )
 
 type FedoraDownload struct {
-	Download
+	machine.Download
 }
 
-func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
+func NewFedoraDownloader(vmType, vmName, releaseStream string) (machine.DistributionDownload, error) {
 	downloadURL, version, arch, size, err := getFedoraDownload()
 	if err != nil {
 		return nil, err
 	}
 
-	cacheDir, err := GetCacheDir(vmType)
+	cacheDir, err := machine.GetCacheDir(vmType)
 	if err != nil {
 		return nil, err
 	}
@@ -40,11 +41,11 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
 	imageName := fmt.Sprintf("fedora-podman-%s-%s.tar.xz", arch, version)
 
 	f := FedoraDownload{
-		Download: Download{
-			Arch:      getFcosArch(),
-			Artifact:  artifact,
+		Download: machine.Download{
+			Arch:      machine.GetFcosArch(),
+			Artifact:  "",
 			CacheDir:  cacheDir,
-			Format:    Format,
+			Format:    machine.Format,
 			ImageName: imageName,
 			LocalPath: filepath.Join(cacheDir, imageName),
 			URL:       downloadURL,
@@ -52,15 +53,15 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
 			Size:      size,
 		},
 	}
-	dataDir, err := GetDataDir(vmType)
+	dataDir, err := machine.GetDataDir(vmType)
 	if err != nil {
 		return nil, err
 	}
-	f.Download.LocalUncompressedFile = f.getLocalUncompressedFile(dataDir)
+	f.Download.LocalUncompressedFile = f.GetLocalUncompressedFile(dataDir)
 	return f, nil
 }
 
-func (f FedoraDownload) Get() *Download {
+func (f FedoraDownload) Get() *machine.Download {
 	return &f.Download
 }
 
@@ -78,12 +79,12 @@ func (f FedoraDownload) HasUsableCache() (bool, error) {
 func (f FedoraDownload) CleanCache() error {
 	// Set cached image to expire after 2 weeks
 	expire := 14 * 24 * time.Hour
-	return removeImageAfterExpire(f.CacheDir, expire)
+	return machine.RemoveImageAfterExpire(f.CacheDir, expire)
 }
 
 func getFedoraDownload() (*url.URL, string, string, int64, error) {
 	var releaseURL string
-	arch := determineFedoraArch()
+	arch := machine.DetermineMachineArch()
 	switch arch {
 	case "arm64":
 		releaseURL = githubArmReleaseURL
diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go
index a42df8f1dc..4a57164e47 100644
--- a/pkg/machine/wsl/machine.go
+++ b/pkg/machine/wsl/machine.go
@@ -415,7 +415,7 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {
 
 	if _, e := strconv.Atoi(opts.ImagePath); e == nil {
 		v.ImageStream = opts.ImagePath
-		dd, err = machine.NewFedoraDownloader(vmtype, v.Name, opts.ImagePath)
+		dd, err = NewFedoraDownloader(vmtype, v.Name, opts.ImagePath)
 	} else {
 		v.ImageStream = "custom"
 		dd, err = machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)