From 0c5cdb35b34336d290edeb921f82253d6e77ea9f Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Tue, 24 Dec 2024 01:09:10 -0600 Subject: [PATCH] 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... --- nix/cache.nix | 1 + nix/cache/cache.go | 52 ++++++++++++---------------------------------- 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/nix/cache.nix b/nix/cache.nix index 0adf7e5..239551e 100644 --- a/nix/cache.nix +++ b/nix/cache.nix @@ -30,6 +30,7 @@ writeShellApplication { ]; text = '' + set -o pipefail function list_isos { '' + (concatStringsSep "\n" ( diff --git a/nix/cache/cache.go b/nix/cache/cache.go index c1850aa..f7e6fae 100644 --- a/nix/cache/cache.go +++ b/nix/cache/cache.go @@ -11,7 +11,6 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" - "github.com/schollz/progressbar/v3" ) func listBucketContents(minioClient *minio.Client, bucketName string) ([]string, error) { @@ -62,36 +61,22 @@ func processStdin() []IsoInfo { 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 { // Create the destination file path 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 resp, err := http.Get(iso.Url) if err != nil { return err } defer resp.Body.Close() - // - // Create download progress bar - downloadBar := progressbar.DefaultBytes( - resp.ContentLength, - "Downloading: "+iso.Url, - ) // Create the destination file destFile, err := os.Create(destPath) @@ -100,11 +85,8 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error { } defer destFile.Close() - // Create a wrapped reader with progress bar - reader := io.TeeReader(resp.Body, downloadBar) - // Copy the downloaded content to the file - _, err = io.Copy(destFile, reader) + _, err = io.Copy(destFile, resp.Body) if err != nil { return err } @@ -122,20 +104,9 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error { return err } - // Create upload progress bar - uploadBar := progressbar.DefaultBytes( - fileInfo.Size(), - "Uploading: "+destPath, - ) - - // Create wrapped reader for upload progress - uploadReader := &CustomReader{ - Reader: uploadFile, - ProgressBar: uploadBar, - } - + log.Println("Uploading " + destPath) // 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", }) if err != nil { @@ -184,7 +155,10 @@ func main() { } if !exists { - downloadAndUploadIso(minioClient, iso) + if err := downloadAndUploadIso(minioClient, iso); err != nil { + log.Println("Error downloading/uploading ISO:", err) + os.Exit(1) + } } } }