Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmds): option for progress bar in cat/get #8686

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 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,15 @@ 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()
schomatis marked this conversation as resolved.
Show resolved Hide resolved
}

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