From 843ee16bac6bb0b1ac815894e7ae11c5058c568a Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Wed, 28 Feb 2024 17:24:47 +0100 Subject: [PATCH] Hide progress bar when not on a TTY --- builder/source/simple.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/builder/source/simple.go b/builder/source/simple.go index eea658d..181eb47 100644 --- a/builder/source/simple.go +++ b/builder/source/simple.go @@ -190,10 +190,31 @@ func (s *SimpleSource) download(destination string) error { }, }, } - resp := client.Do(req) - // Setup our progress bar + // Show our progress bar + s.showProgress(resp) + + if err := resp.Err(); err != nil { + slog.Error("Error downloading", "uri", s.URI, "err", err) + + return err + } + + return nil +} + +func onTTY() bool { + s, _ := os.Stdout.Stat() + + return s.Mode()&os.ModeCharDevice > 0 +} + +func (s *SimpleSource) showProgress(resp *grab.Response) { + if !onTTY() { + slog.Info("Downloading source", "uri", s.URI) + } + pbar := pb.Start64(resp.Size()) pbar.Set(pb.Bytes, true) pbar.SetTemplateString(progressBarTemplate) @@ -213,12 +234,7 @@ func (s *SimpleSource) download(destination string) error { // Ensure progressbar completes to 100% pbar.SetCurrent(resp.BytesComplete()) - if err := resp.Err(); err != nil { - slog.Error("Error downloading", "uri", s.URI, "err", err) - return err - } - - return nil + return } } }