Skip to content

Commit 7b24c3b

Browse files
committed
pkg/downloader: split pkg/progressbar
Signed-off-by: Akihiro Suda <[email protected]>
1 parent f10a6d2 commit 7b24c3b

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

pkg/downloader/downloader.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ import (
1212
"path"
1313
"path/filepath"
1414
"strings"
15-
"time"
1615

1716
"github.com/cheggaaa/pb/v3"
1817
"github.com/containerd/continuity/fs"
1918
"github.com/lima-vm/lima/pkg/localpathutil"
20-
"github.com/mattn/go-isatty"
19+
"github.com/lima-vm/lima/pkg/progressbar"
2120
"github.com/opencontainers/go-digest"
2221
"github.com/sirupsen/logrus"
2322
)
2423

24+
// HideProgress is used only for testing
2525
var HideProgress bool
2626

27+
// hideBar is used only for testing
28+
func hideBar(bar *pb.ProgressBar) {
29+
bar.Set(pb.ReturnSymbol, "")
30+
bar.SetTemplateString("")
31+
}
32+
2733
type Status = string
2834

2935
const (
@@ -320,10 +326,13 @@ func decompressLocal(dst, src, ext string, description string) error {
320326
if err != nil {
321327
return err
322328
}
323-
bar, err := createBar(st.Size())
329+
bar, err := progressbar.New(st.Size())
324330
if err != nil {
325331
return err
326332
}
333+
if HideProgress {
334+
hideBar(bar)
335+
}
327336

328337
in, err := os.Open(src)
329338
if err != nil {
@@ -384,30 +393,6 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
384393
return nil
385394
}
386395

387-
func createBar(size int64) (*pb.ProgressBar, error) {
388-
bar := pb.New64(size)
389-
390-
bar.Set(pb.Bytes, true)
391-
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
392-
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
393-
bar.SetRefreshRate(200 * time.Millisecond)
394-
} else if HideProgress {
395-
bar.Set(pb.ReturnSymbol, "")
396-
bar.SetTemplateString("")
397-
} else {
398-
bar.Set(pb.Terminal, false)
399-
bar.Set(pb.ReturnSymbol, "\n")
400-
bar.SetTemplateString(`{{counters . }} ({{percent .}}) {{speed . "%s/s"}}`)
401-
bar.SetRefreshRate(5 * time.Second)
402-
}
403-
bar.SetWidth(80)
404-
if err := bar.Err(); err != nil {
405-
return nil, err
406-
}
407-
408-
return bar, nil
409-
}
410-
411396
func downloadHTTP(localPath, url string, description string, expectedDigest digest.Digest) error {
412397
if localPath == "" {
413398
return fmt.Errorf("downloadHTTP: got empty localPath")
@@ -431,10 +416,13 @@ func downloadHTTP(localPath, url string, description string, expectedDigest dige
431416
if resp.StatusCode != http.StatusOK {
432417
return fmt.Errorf("expected HTTP status %d, got %s", http.StatusOK, resp.Status)
433418
}
434-
bar, err := createBar(resp.ContentLength)
419+
bar, err := progressbar.New(resp.ContentLength)
435420
if err != nil {
436421
return err
437422
}
423+
if HideProgress {
424+
hideBar(bar)
425+
}
438426

439427
writers := []io.Writer{fileWriter}
440428
var digester digest.Digester

pkg/progressbar/progressbar.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package progressbar
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/cheggaaa/pb/v3"
8+
"github.com/mattn/go-isatty"
9+
)
10+
11+
func New(size int64) (*pb.ProgressBar, error) {
12+
bar := pb.New64(size)
13+
14+
bar.Set(pb.Bytes, true)
15+
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
16+
bar.SetTemplateString(`{{counters . }} {{bar . | green }} {{percent .}} {{speed . "%s/s"}}`)
17+
bar.SetRefreshRate(200 * time.Millisecond)
18+
} else {
19+
bar.Set(pb.Terminal, false)
20+
bar.Set(pb.ReturnSymbol, "\n")
21+
bar.SetTemplateString(`{{counters . }} ({{percent .}}) {{speed . "%s/s"}}`)
22+
bar.SetRefreshRate(5 * time.Second)
23+
}
24+
bar.SetWidth(80)
25+
if err := bar.Err(); err != nil {
26+
return nil, err
27+
}
28+
29+
return bar, nil
30+
}

0 commit comments

Comments
 (0)