Skip to content

Commit

Permalink
fix(cmds): option for progress bar in cat/get (#8686)
Browse files Browse the repository at this point in the history
* fix(cmds): option for progress bar in cat/get

* defer bar.Finish()
  • Loading branch information
schomatis authored Mar 11, 2022
1 parent eafa1c7 commit 199659a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
14 changes: 12 additions & 2 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ipfs/go-ipfs/core/commands/cmdenv"

"github.com/cheggaaa/pb"
"github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/go-ipfs-files"
"github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -32,6 +33,7 @@ var CatCmd = &cmds.Command{
Options: []cmds.Option{
cmds.Int64Option(offsetOptionName, "o", "Byte offset to begin reading from."),
cmds.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
cmds.BoolOption(progressOptionName, "p", "Stream progress data.").WithDefault(true),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
Expand Down Expand Up @@ -96,8 +98,16 @@ var CatCmd = &cmds.Command{

switch val := v.(type) {
case io.Reader:
bar, reader := progressBarForReader(os.Stderr, val, int64(res.Length()))
bar.Start()
reader := val

req := res.Request()
progress, _ := req.Options[progressOptionName].(bool)
if progress {
var bar *pb.ProgressBar
bar, reader = progressBarForReader(os.Stderr, val, int64(res.Length()))
bar.Start()
defer bar.Finish()
}

err = re.Emit(reader)
if err != nil {
Expand Down
29 changes: 20 additions & 9 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
cmds.BoolOption(archiveOptionName, "a", "Output a TAR archive."),
cmds.BoolOption(compressOptionName, "C", "Compress the output with GZIP compression."),
cmds.IntOption(compressionLevelOptionName, "l", "The level of compression (1-9)."),
cmds.BoolOption(progressOptionName, "p", "Stream progress data.").WithDefault(true),
},
PreRun: func(req *cmds.Request, env cmds.Environment) error {
_, err := getCompressOptions(req)
Expand Down Expand Up @@ -114,13 +115,15 @@ may also specify the level of compression by specifying '-l=<1-9>'.
}

archive, _ := req.Options[archiveOptionName].(bool)
progress, _ := req.Options[progressOptionName].(bool)

gw := getWriter{
Out: os.Stdout,
Err: os.Stderr,
Archive: archive,
Compression: cmplvl,
Size: int64(res.Length()),
Progress: progress,
}

return gw.Write(outReader, outPath)
Expand Down Expand Up @@ -181,6 +184,7 @@ type getWriter struct {
Archive bool
Compression int
Size int64
Progress bool
}

func (gw *getWriter) Write(r io.Reader, fpath string) error {
Expand Down Expand Up @@ -213,22 +217,29 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {
defer file.Close()

fmt.Fprintf(gw.Out, "Saving archive to %s\n", fpath)
bar, barR := progressBarForReader(gw.Err, r, gw.Size)
bar.Start()
defer bar.Finish()
if gw.Progress {
var bar *pb.ProgressBar
bar, r = progressBarForReader(gw.Err, r, gw.Size)
bar.Start()
defer bar.Finish()
}

_, err = io.Copy(file, barR)
_, err = io.Copy(file, r)
return err
}

func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
bar := makeProgressBar(gw.Err, gw.Size)
bar.Start()
defer bar.Finish()
defer bar.Set64(gw.Size)
var progressCb func(int64) int64
if gw.Progress {
bar := makeProgressBar(gw.Err, gw.Size)
bar.Start()
defer bar.Finish()
defer bar.Set64(gw.Size)
progressCb = bar.Add64
}

extractor := &tar.Extractor{Path: fpath, Progress: bar.Add64}
extractor := &tar.Extractor{Path: fpath, Progress: progressCb}
return extractor.Extract(r)
}

Expand Down

0 comments on commit 199659a

Please sign in to comment.