Merge pull request #695 from harshavardhana/pr_out_add_barmsg_to_carry_error_method_to_purge_erroneous_size_upon_cp_error

This commit is contained in:
Harshavardhana
2015-05-25 12:43:13 -07:00
3 changed files with 25 additions and 5 deletions
+10 -3
View File
@@ -19,6 +19,7 @@ package main
import (
"errors"
"fmt"
"io"
"runtime"
"sync"
@@ -33,18 +34,21 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target
if err != nil {
return iodine.New(err, nil)
}
var newReader io.Reader
switch globalQuietFlag {
case true:
console.Infoln(fmt.Sprintf("‘%s’ -> ‘%s’", sourceURL, targetURL))
newReader = reader
default:
// set up progress
reader = bar.NewProxyReader(reader)
newReader = bar.NewProxyReader(reader)
}
err = putTarget(targetURL, targetConfig, length, reader)
err = putTarget(targetURL, targetConfig, length, newReader)
if err != nil {
reader.Close()
return iodine.New(err, nil)
}
reader.Close()
return nil
}
@@ -146,14 +150,17 @@ func runCopyCmd(ctx *cli.Context) {
if !globalQuietFlag {
bar = newCpBar()
}
for err := range doCopyCmd(sourceURLs, targetURL, bar) {
if err != nil {
console.Errorln(ErrorMessage{
Message: "Failed with",
Error: iodine.New(err, nil),
})
bar.Error()
}
}
if !globalQuietFlag {
bar.Finish()
}
+1 -1
View File
@@ -28,7 +28,7 @@ import (
)
// getSource -
func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.Reader, length uint64, err error) {
func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length uint64, err error) {
sourceClnt, err := getNewClient(sourceURL, sourceConfig)
if err != nil {
return nil, 0, iodine.New(err, map[string]string{"failedURL": sourceURL})
+14 -1
View File
@@ -31,6 +31,7 @@ const (
cpBarCmdExtend cpBarCmd = iota
cpBarCmdProgress
cpBarCmdFinish
cpBarCmdError
)
type copyReader struct {
@@ -62,6 +63,10 @@ func (b barSend) Progress(progress int64) {
b.cmdCh <- barMsg{Cmd: cpBarCmdProgress, Arg: progress}
}
func (b barSend) Error() {
b.cmdCh <- barMsg{Cmd: cpBarCmdError}
}
func (b *barSend) NewProxyReader(r io.Reader) *copyReader {
return &copyReader{r, b}
}
@@ -78,6 +83,8 @@ func newCpBar() barSend {
finishCh := make(chan bool)
go func(cmdCh <-chan barMsg, finishCh chan<- bool) {
started := false
var totalBytesRead int64
var currentSizeRead int64
bar := pb.New64(0)
bar.SetUnits(pb.U_BYTES)
bar.SetRefreshRate(time.Millisecond * 10)
@@ -98,7 +105,13 @@ func newCpBar() barSend {
bar.Start()
}
case cpBarCmdProgress:
bar.Add64(msg.Arg.(int64))
if msg.Arg.(int64) > 0 {
currentSizeRead = msg.Arg.(int64)
totalBytesRead += currentSizeRead
bar.Add64(currentSizeRead)
}
case cpBarCmdError:
bar.Set64(totalBytesRead - currentSizeRead)
case cpBarCmdFinish:
if started {
bar.Finish()