2019-08-05 19:47:11 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-08-05 18:46:38 +00:00
|
|
|
// 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
|
2019-08-15 00:28:38 +00:00
|
|
|
|
2019-08-05 18:46:38 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2019-08-08 22:32:29 +00:00
|
|
|
"github.com/cheggaaa/pb/v3"
|
2019-08-05 18:46:38 +00:00
|
|
|
"github.com/hashicorp/go-getter"
|
|
|
|
)
|
|
|
|
|
2019-09-20 15:55:40 +00:00
|
|
|
// DefaultProgressBar is the default cheggaaa progress bar
|
2019-08-15 00:28:38 +00:00
|
|
|
var DefaultProgressBar getter.ProgressTracker = &progressBar{}
|
2019-08-05 18:46:38 +00:00
|
|
|
|
|
|
|
type progressBar struct {
|
2019-08-08 22:32:29 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
progress *pb.ProgressBar
|
2019-08-05 18:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2019-08-08 22:32:29 +00:00
|
|
|
if cpb.progress == nil {
|
|
|
|
cpb.progress = pb.New64(totalSize)
|
2019-08-05 18:46:38 +00:00
|
|
|
}
|
2019-08-10 07:18:49 +00:00
|
|
|
p := pb.Full.Start64(totalSize)
|
2019-09-18 00:47:45 +00:00
|
|
|
p.Set("prefix", " > "+filepath.Base(src+": "))
|
2019-08-08 22:32:29 +00:00
|
|
|
p.SetCurrent(currentSize)
|
|
|
|
p.Set(pb.Bytes, true)
|
2019-08-05 18:46:38 +00:00
|
|
|
|
2019-08-05 23:32:54 +00:00
|
|
|
// Just a hair less than 80 (standard terminal width) for aesthetics & pasting into docs
|
2019-08-08 22:32:29 +00:00
|
|
|
p.SetWidth(79)
|
|
|
|
barReader := p.NewProxyReader(stream)
|
2019-08-05 18:46:38 +00:00
|
|
|
|
|
|
|
return &readCloser{
|
2019-08-08 22:32:29 +00:00
|
|
|
Reader: barReader,
|
2019-08-05 18:46:38 +00:00
|
|
|
close: func() error {
|
|
|
|
cpb.lock.Lock()
|
|
|
|
defer cpb.lock.Unlock()
|
|
|
|
p.Finish()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readCloser struct {
|
|
|
|
io.Reader
|
|
|
|
close func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *readCloser) Close() error { return c.close() }
|