@@ -12,18 +12,24 @@ import (
12
12
"path"
13
13
"path/filepath"
14
14
"strings"
15
- "time"
16
15
17
16
"github.com/cheggaaa/pb/v3"
18
17
"github.com/containerd/continuity/fs"
19
18
"github.com/lima-vm/lima/pkg/localpathutil"
20
- "github.com/mattn/go-isatty "
19
+ "github.com/lima-vm/lima/pkg/progressbar "
21
20
"github.com/opencontainers/go-digest"
22
21
"github.com/sirupsen/logrus"
23
22
)
24
23
24
+ // HideProgress is used only for testing
25
25
var HideProgress bool
26
26
27
+ // hideBar is used only for testing
28
+ func hideBar (bar * pb.ProgressBar ) {
29
+ bar .Set (pb .ReturnSymbol , "" )
30
+ bar .SetTemplateString ("" )
31
+ }
32
+
27
33
type Status = string
28
34
29
35
const (
@@ -320,10 +326,13 @@ func decompressLocal(dst, src, ext string, description string) error {
320
326
if err != nil {
321
327
return err
322
328
}
323
- bar , err := createBar (st .Size ())
329
+ bar , err := progressbar . New (st .Size ())
324
330
if err != nil {
325
331
return err
326
332
}
333
+ if HideProgress {
334
+ hideBar (bar )
335
+ }
327
336
328
337
in , err := os .Open (src )
329
338
if err != nil {
@@ -384,30 +393,6 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
384
393
return nil
385
394
}
386
395
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
-
411
396
func downloadHTTP (localPath , url string , description string , expectedDigest digest.Digest ) error {
412
397
if localPath == "" {
413
398
return fmt .Errorf ("downloadHTTP: got empty localPath" )
@@ -431,10 +416,13 @@ func downloadHTTP(localPath, url string, description string, expectedDigest dige
431
416
if resp .StatusCode != http .StatusOK {
432
417
return fmt .Errorf ("expected HTTP status %d, got %s" , http .StatusOK , resp .Status )
433
418
}
434
- bar , err := createBar (resp .ContentLength )
419
+ bar , err := progressbar . New (resp .ContentLength )
435
420
if err != nil {
436
421
return err
437
422
}
423
+ if HideProgress {
424
+ hideBar (bar )
425
+ }
438
426
439
427
writers := []io.Writer {fileWriter }
440
428
var digester digest.Digester
0 commit comments