fix(cmds): option for progress bar in cat/get (#8686)
* fix(cmds): option for progress bar in cat/get * defer bar.Finish()
Lucas Molas committed
Mar 11, 2022 at 16:25 UTC
199659ab77235bbe199392849b92309df13115ec
2 files changed
+32
-11
core/commands/cat.go
+12
-2
@@ -8,6 +8,7 @@ import (
8
9
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
11
+ "github.com/cheggaaa/pb"
12
"github.com/ipfs/go-ipfs-cmds"
13
"github.com/ipfs/go-ipfs-files"
14
"github.com/ipfs/interface-go-ipfs-core"
@@ -32,6 +33,7 @@ var CatCmd = &cmds.Command{
33
Options: []cmds.Option{
34
cmds.Int64Option(offsetOptionName, "o", "Byte offset to begin reading from."),
35
cmds.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
36
+ cmds.BoolOption(progressOptionName, "p", "Stream progress data.").WithDefault(true),
37
},
38
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
39
api, err := cmdenv.GetApi(env, req)
@@ -96,8 +98,16 @@ var CatCmd = &cmds.Command{
98
99
switch val := v.(type) {
100
case io.Reader:
99
- bar, reader := progressBarForReader(os.Stderr, val, int64(res.Length()))
100
- bar.Start()
101
+ reader := val
102
+
103
+ req := res.Request()
104
+ progress, _ := req.Options[progressOptionName].(bool)
105
+ if progress {
106
+ var bar *pb.ProgressBar
107
+ bar, reader = progressBarForReader(os.Stderr, val, int64(res.Length()))
108
+ bar.Start()
109
+ defer bar.Finish()
110
+ }
111
112
err = re.Emit(reader)
113
if err != nil {
core/commands/get.go
+20
-9
@@ -54,6 +54,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
54
cmds.BoolOption(archiveOptionName, "a", "Output a TAR archive."),
55
cmds.BoolOption(compressOptionName, "C", "Compress the output with GZIP compression."),
56
cmds.IntOption(compressionLevelOptionName, "l", "The level of compression (1-9)."),
57
+ cmds.BoolOption(progressOptionName, "p", "Stream progress data.").WithDefault(true),
58
},
59
PreRun: func(req *cmds.Request, env cmds.Environment) error {
60
_, err := getCompressOptions(req)
@@ -114,6 +115,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
115
}
116
117
archive, _ := req.Options[archiveOptionName].(bool)
118
+ progress, _ := req.Options[progressOptionName].(bool)
119
120
gw := getWriter{
121
Out: os.Stdout,
@@ -121,6 +123,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
123
Archive: archive,
124
Compression: cmplvl,
125
Size: int64(res.Length()),
126
+ Progress: progress,
127
}
128
129
return gw.Write(outReader, outPath)
@@ -181,6 +184,7 @@ type getWriter struct {
184
Archive bool
185
Compression int
186
Size int64
187
+ Progress bool
188
}
189
190
func (gw *getWriter) Write(r io.Reader, fpath string) error {
@@ -213,22 +217,29 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
217
defer file.Close()
218
219
fmt.Fprintf(gw.Out, "Saving archive to %s\n", fpath)
216
- bar, barR := progressBarForReader(gw.Err, r, gw.Size)
217
- bar.Start()
218
- defer bar.Finish()
220
+ if gw.Progress {
221
+ var bar *pb.ProgressBar
222
+ bar, r = progressBarForReader(gw.Err, r, gw.Size)
223
+ bar.Start()
224
+ defer bar.Finish()
225
+ }
226
220
- _, err = io.Copy(file, barR)
227
+ _, err = io.Copy(file, r)
228
return err
229
}
230
231
func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
232
fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
226
- bar := makeProgressBar(gw.Err, gw.Size)
227
- bar.Start()
228
- defer bar.Finish()
229
- defer bar.Set64(gw.Size)
233
+ var progressCb func(int64) int64
234
+ if gw.Progress {
235
+ bar := makeProgressBar(gw.Err, gw.Size)
236
+ bar.Start()
237
+ defer bar.Finish()
238
+ defer bar.Set64(gw.Size)
239
+ progressCb = bar.Add64
240
+ }
241
231
- extractor := &tar.Extractor{Path: fpath, Progress: bar.Add64}
242
+ extractor := &tar.Extractor{Path: fpath, Progress: progressCb}
243
return extractor.Extract(r)
244
}
245