From 6bf29205bc236987ea2557efdf34611e1f3186cd Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 11 Jan 2018 12:39:47 -0800 Subject: [PATCH] remove progress bar size hack Compute the size in the PostCmd instead of the PreCmd. License: MIT Signed-off-by: Steven Allen --- core/commands/add.go | 48 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/core/commands/add.go b/core/commands/add.go index aa2b50bb1..6166a4c28 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -1,7 +1,6 @@ package commands import ( - "context" "errors" "fmt" "io" @@ -138,28 +137,6 @@ You can now check what blocks have been created by: req.Options[progressOptionName] = true } - sizeFile, ok := req.Files.(files.SizeFile) - if !ok { - // we don't need to error, the progress bar just won't know how big the files are - log.Warning("cannot determine size of input file") - return nil - } - - // HACK! Using context to pass the size to PostRun - sizeCh := make(chan int64, 1) - req.Context = context.WithValue(req.Context, "size", sizeCh) - - go func() { - size, err := sizeFile.Size() - if err != nil { - log.Warningf("error getting files size: %s", err) - // see comment above - return - } - - sizeCh <- size - }() - return nil }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) { @@ -348,6 +325,27 @@ You can now check what blocks have been created by: reNext, res := cmds.NewChanResponsePair(req) outChan := make(chan interface{}) + sizeChan := make(chan int64, 1) + + sizeFile, ok := req.Files.(files.SizeFile) + if ok { + // Could be slow. + go func() { + size, err := sizeFile.Size() + if err != nil { + log.Warningf("error getting files size: %s", err) + // see comment above + return + } + + sizeChan <- size + }() + } else { + // we don't need to error, the progress bar just + // won't know how big the files are + log.Warning("cannot determine size of input file") + } + progressBar := func(wait chan struct{}) { defer close(wait) @@ -367,10 +365,6 @@ You can now check what blocks have been created by: bar.Start() } - // HACK! using context to pass size from PreRun - var sizeChan chan int64 - sizeChan, _ = req.Context.Value("size").(chan int64) - lastFile := "" lastHash := "" var totalProgress, prevFiles, lastBytes int64