Skip to content

Commit

Permalink
Hide progress bar when not on a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
silkeh committed Feb 28, 2024
1 parent 24a2fa5 commit 843ee16
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions builder/source/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 843ee16

Please sign in to comment.