diff --git a/cmd-cp.go b/cmd-cp.go index fb9709dc..291e7606 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -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() } diff --git a/common-methods.go b/common-methods.go index 8e8b61e1..e509bc54 100644 --- a/common-methods.go +++ b/common-methods.go @@ -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}) diff --git a/cp-bar.go b/cp-bar.go index f71df241..7d9f9239 100644 --- a/cp-bar.go +++ b/cp-bar.go @@ -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 ©Reader{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()