Remove the progress bar and fix the downloader

The progress bar *might* be causing problems during the upload. So we
remove it and allow the progress to be mysterious to the user

Return a non-zero error code if there is a failure

Make the cache directory first...
This commit is contained in:
Greg Hellings
2024-12-24 01:22:53 -06:00
parent f75558bfb7
commit 0c5cdb35b3
2 changed files with 14 additions and 39 deletions
+1
View File
@@ -30,6 +30,7 @@ writeShellApplication {
]; ];
text = text =
'' ''
set -o pipefail
function list_isos { function list_isos {
'' ''
+ (concatStringsSep "\n" ( + (concatStringsSep "\n" (
+13 -39
View File
@@ -11,7 +11,6 @@ import (
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
"github.com/schollz/progressbar/v3"
) )
func listBucketContents(minioClient *minio.Client, bucketName string) ([]string, error) { func listBucketContents(minioClient *minio.Client, bucketName string) ([]string, error) {
@@ -62,36 +61,22 @@ func processStdin() []IsoInfo {
return isos return isos
} }
// CustomReader wraps an io.Reader with a progress bar
type CustomReader struct {
Reader io.Reader
ProgressBar *progressbar.ProgressBar
}
func (r *CustomReader) Read(p []byte) (int, error) {
n, err := r.Reader.Read(p)
if n > 0 {
r.ProgressBar.Add(n)
}
return n, err
}
func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error { func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
// Create the destination file path // Create the destination file path
destPath := "cache/" + iso.Hash + ".iso" destPath := "cache/" + iso.Hash + ".iso"
// Create cache directory if it doesn't exist
err := os.MkdirAll("cache", 0755)
if err != nil {
return err
}
log.Println("Downloading " + iso.Url)
// Download the ISO file // Download the ISO file
resp, err := http.Get(iso.Url) resp, err := http.Get(iso.Url)
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
//
// Create download progress bar
downloadBar := progressbar.DefaultBytes(
resp.ContentLength,
"Downloading: "+iso.Url,
)
// Create the destination file // Create the destination file
destFile, err := os.Create(destPath) destFile, err := os.Create(destPath)
@@ -100,11 +85,8 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
} }
defer destFile.Close() defer destFile.Close()
// Create a wrapped reader with progress bar
reader := io.TeeReader(resp.Body, downloadBar)
// Copy the downloaded content to the file // Copy the downloaded content to the file
_, err = io.Copy(destFile, reader) _, err = io.Copy(destFile, resp.Body)
if err != nil { if err != nil {
return err return err
} }
@@ -122,20 +104,9 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
return err return err
} }
// Create upload progress bar log.Println("Uploading " + destPath)
uploadBar := progressbar.DefaultBytes(
fileInfo.Size(),
"Uploading: "+destPath,
)
// Create wrapped reader for upload progress
uploadReader := &CustomReader{
Reader: uploadFile,
ProgressBar: uploadBar,
}
// Upload the file to MinIO // Upload the file to MinIO
_, err = minioClient.PutObject(context.Background(), "isos", "cache/"+iso.Hash+".iso", uploadReader, fileInfo.Size(), minio.PutObjectOptions{ _, err = minioClient.PutObject(context.Background(), "isos", "cache/"+iso.Hash+".iso", uploadFile, fileInfo.Size(), minio.PutObjectOptions{
ContentType: "application/x-iso9660-image", ContentType: "application/x-iso9660-image",
}) })
if err != nil { if err != nil {
@@ -184,7 +155,10 @@ func main() {
} }
if !exists { if !exists {
downloadAndUploadIso(minioClient, iso) if err := downloadAndUploadIso(minioClient, iso); err != nil {
log.Println("Error downloading/uploading ISO:", err)
os.Exit(1)
}
} }
} }
} }