Skip to content

Commit 53f80c3

Browse files
author
rht
committed
Fix progress bar for "get" command
1 parent 13cd226 commit 53f80c3

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

core/commands/get.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ may also specify the level of compression by specifying '-l=<1-9>'.
6363
return
6464
}
6565

66-
reader, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl)
66+
reader, length, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl)
6767
if err != nil {
6868
res.SetError(err, cmds.ErrNormal)
6969
return
7070
}
71+
res.SetLength(length)
7172
res.SetOutput(reader)
7273
},
7374
PostRun: func(req cmds.Request, res cmds.Response) {
@@ -105,8 +106,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
105106
}
106107
defer file.Close()
107108

108-
bar := pb.New(0).SetUnits(pb.U_BYTES)
109-
bar.Output = os.Stderr
109+
bar := pb.New64(int64(res.Length())).SetUnits(pb.U_BYTES)
110+
bar.Output = res.Stderr()
110111
pbReader := bar.NewProxyReader(outReader)
111112
bar.Start()
112113
defer bar.Finish()
@@ -122,9 +123,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
122123

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

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

129129
// wrap the reader with the progress bar proxy reader
130130
// if the output is compressed, also wrap it in a gzip.Reader
@@ -144,6 +144,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
144144
bar.Start()
145145
defer bar.Finish()
146146

147+
//res.SetOutput(reader)
148+
147149
extractor := &tar.Extractor{outPath}
148150
err = extractor.Extract(reader)
149151
if err != nil {
@@ -166,12 +168,20 @@ func getCompressOptions(req cmds.Request) (int, error) {
166168
return gzip.NoCompression, nil
167169
}
168170

169-
func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, error) {
171+
func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, uint64, error) {
170172
pathToResolve := path.Path(p)
171173
dagnode, err := core.Resolve(ctx, node, pathToResolve)
172174
if err != nil {
173-
return nil, err
175+
return nil, 0, err
174176
}
175177

176-
return utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
178+
reader, err := utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
179+
ns, err := dagnode.Stat()
180+
if err != nil {
181+
return nil, 0, err
182+
}
183+
184+
length := uint64(ns.CumulativeSize)
185+
186+
return reader, length, err
177187
}

0 commit comments

Comments
 (0)