Skip to content

Commit

Permalink
Fix progress bar for "get" command
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed May 30, 2015
1 parent 6f15523 commit 1170fd2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 19 additions & 11 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ may also specify the level of compression by specifying '-l=<1-9>'.
return
}

reader, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl)
reader, length, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetLength(length)
res.SetOutput(reader)
},
PostRun: func(req cmds.Request, res cmds.Response) {
Expand Down Expand Up @@ -105,11 +106,10 @@ may also specify the level of compression by specifying '-l=<1-9>'.
}
defer file.Close()

bar := pb.New(0).SetUnits(pb.U_BYTES)
bar.Output = os.Stderr
bar := pb.New64(int64(res.Length())).SetUnits(pb.U_BYTES)
bar.Output = res.Stderr()
pbReader := bar.NewProxyReader(outReader)
bar.Start()
defer bar.Finish()

_, err = io.Copy(file, pbReader)
if err != nil {
Expand All @@ -122,9 +122,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.

fmt.Printf("Saving file(s) to %s\n", outPath)

// TODO: get total length of files
bar := pb.New(0).SetUnits(pb.U_BYTES)
bar.Output = os.Stderr
bar := pb.New64(int64(res.Length())).SetUnits(pb.U_BYTES)
bar.Output = res.Stderr()

// wrap the reader with the progress bar proxy reader
// if the output is compressed, also wrap it in a gzip.Reader
Expand All @@ -142,7 +141,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
}

bar.Start()
defer bar.Finish()

//res.SetOutput(reader)

extractor := &tar.Extractor{outPath}
err = extractor.Extract(reader)
Expand All @@ -166,12 +166,20 @@ func getCompressOptions(req cmds.Request) (int, error) {
return gzip.NoCompression, nil
}

func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, error) {
func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, uint64, error) {
pathToResolve := path.Path(p)
dagnode, err := core.Resolve(ctx, node, pathToResolve)
if err != nil {
return nil, err
return nil, 0, err
}

return utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
reader, err := utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
ns, err := dagnode.Stat()
if err != nil {
return nil, 0, err
}

length := uint64(ns.CumulativeSize)

return reader, length, err
}
4 changes: 2 additions & 2 deletions test/sharness/t0090-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_get_cmd() {
'

test_expect_success "ipfs get succeeds" '
echo "Hello Worlds!" >data &&
echo "Hello Worlds\!" >data &&
HASH=`ipfs add -q data` &&
ipfs get "$HASH" >actual
'
Expand Down Expand Up @@ -80,7 +80,7 @@ test_get_cmd() {
mkdir -p dir &&
touch dir/a &&
mkdir -p dir/b &&
echo "Hello, Worlds!" >dir/b/c &&
echo "Hello, Worlds\!" >dir/b/c &&
HASH2=`ipfs add -r -q dir | tail -n 1` &&
ipfs get "$HASH2" >actual
'
Expand Down

0 comments on commit 1170fd2

Please sign in to comment.