-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
progressbar.go
90 lines (74 loc) · 2.13 KB
/
progressbar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This file implements a go-getter wrapper for cheggaaa progress bar
// based on:
// https://github.com/hashicorp/go-getter/blob/master/cmd/go-getter/progress_tracking.go
package util
import (
"fmt"
"io"
"path/filepath"
"sync"
"github.com/cheggaaa/pb"
"github.com/golang/glog"
"github.com/hashicorp/go-getter"
)
var defaultProgressBar getter.ProgressTracker = &progressBar{}
type progressBar struct {
lock sync.Mutex
pool *pb.Pool
pbs int
}
// TrackProgress instantiates a new progress bar that will
// display the progress of stream until closed.
// total can be 0.
func (cpb *progressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser {
cpb.lock.Lock()
defer cpb.lock.Unlock()
if cpb.pool == nil {
cpb.pool = pb.NewPool()
if err := cpb.pool.Start(); err != nil {
glog.Errorf("pool start: %v", err)
}
}
p := pb.New64(totalSize)
p.Set64(currentSize)
p.SetUnits(pb.U_BYTES)
p.Prefix(fmt.Sprintf(" %s:", filepath.Base(src)))
// Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs
p.SetWidth(78)
cpb.pool.Add(p)
reader := p.NewProxyReader(stream)
cpb.pbs++
return &readCloser{
Reader: reader,
close: func() error {
cpb.lock.Lock()
defer cpb.lock.Unlock()
p.Finish()
cpb.pbs--
if cpb.pbs <= 0 {
if err := cpb.pool.Stop(); err != nil {
glog.Errorf("pool stop: %v", err)
}
cpb.pool = nil
}
return nil
},
}
}
type readCloser struct {
io.Reader
close func() error
}
func (c *readCloser) Close() error { return c.close() }