Merge pull request #14962 from n1hility/improve-fetch-message
Improve download message on Windows
This commit is contained in:
		
						commit
						6947746bbb
					
				| 
						 | 
				
			
			@ -6,7 +6,10 @@ package machine
 | 
			
		|||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
| 
						 | 
				
			
			@ -23,7 +26,7 @@ type FedoraDownload struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
 | 
			
		||||
	downloadURL, size, err := getFedoraDownload(githubLatestReleaseURL)
 | 
			
		||||
	downloadURL, version, size, err := getFedoraDownload(githubLatestReleaseURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -33,7 +36,7 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
 | 
			
		|||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	imageName := "rootfs.tar.xz"
 | 
			
		||||
	imageName := fmt.Sprintf("fedora-podman-%s.tar.xz", version)
 | 
			
		||||
 | 
			
		||||
	f := FedoraDownload{
 | 
			
		||||
		Download: Download{
 | 
			
		||||
| 
						 | 
				
			
			@ -77,21 +80,36 @@ func (f FedoraDownload) CleanCache() error {
 | 
			
		|||
	return removeImageAfterExpire(f.CacheDir, expire)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getFedoraDownload(releaseURL string) (*url.URL, int64, error) {
 | 
			
		||||
func getFedoraDownload(releaseURL string) (*url.URL, string, int64, error) {
 | 
			
		||||
	downloadURL, err := url.Parse(releaseURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, -1, fmt.Errorf("invalid URL generated from discovered Fedora file: %s: %w", releaseURL, err)
 | 
			
		||||
		return nil, "", -1, fmt.Errorf("invalid URL generated from discovered Fedora file: %s: %w", releaseURL, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp, err := http.Head(releaseURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
 | 
			
		||||
		return nil, "", -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
 | 
			
		||||
	}
 | 
			
		||||
	_ = resp.Body.Close()
 | 
			
		||||
	contentLen := resp.ContentLength
 | 
			
		||||
 | 
			
		||||
	if resp.StatusCode != http.StatusOK {
 | 
			
		||||
		return nil, "", -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	verURL := *downloadURL
 | 
			
		||||
	verURL.Path = path.Join(path.Dir(downloadURL.Path), "version")
 | 
			
		||||
 | 
			
		||||
	resp, err = http.Get(verURL.String())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, "", -1, fmt.Errorf("get request failed: %s: %w", verURL.String(), err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bytes, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1024})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, "", -1, fmt.Errorf("failed reading: %s: %w", verURL.String(), err)
 | 
			
		||||
	}
 | 
			
		||||
	_ = resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	if resp.StatusCode != http.StatusOK {
 | 
			
		||||
		return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return downloadURL, resp.ContentLength, nil
 | 
			
		||||
	return downloadURL, strings.TrimSpace(string(bytes)), contentLen, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -113,7 +113,7 @@ func DownloadImage(d DistributionDownload) error {
 | 
			
		|||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if !ok {
 | 
			
		||||
		if err := DownloadVMImage(d.Get().URL, d.Get().LocalPath); err != nil {
 | 
			
		||||
		if err := DownloadVMImage(d.Get().URL, d.Get().ImageName, d.Get().LocalPath); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		// Clean out old cached images, since we didn't find needed image in cache
 | 
			
		||||
| 
						 | 
				
			
			@ -128,7 +128,7 @@ func DownloadImage(d DistributionDownload) error {
 | 
			
		|||
 | 
			
		||||
// DownloadVMImage downloads a VM image from url to given path
 | 
			
		||||
// with download status
 | 
			
		||||
func DownloadVMImage(downloadURL *url2.URL, localImagePath string) error {
 | 
			
		||||
func DownloadVMImage(downloadURL *url2.URL, imageName string, localImagePath string) error {
 | 
			
		||||
	out, err := os.Create(localImagePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
| 
						 | 
				
			
			@ -153,8 +153,7 @@ func DownloadVMImage(downloadURL *url2.URL, localImagePath string) error {
 | 
			
		|||
		return fmt.Errorf("downloading VM image %s: %s", downloadURL, resp.Status)
 | 
			
		||||
	}
 | 
			
		||||
	size := resp.ContentLength
 | 
			
		||||
	urlSplit := strings.Split(downloadURL.Path, "/")
 | 
			
		||||
	prefix := "Downloading VM image: " + urlSplit[len(urlSplit)-1]
 | 
			
		||||
	prefix := "Downloading VM image: " + imageName
 | 
			
		||||
	onComplete := prefix + ": done"
 | 
			
		||||
 | 
			
		||||
	p := mpb.New(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue