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

Add ipfs add --quieter #3770

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 18 additions & 11 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

const (
quietOptionName = "quiet"
quieterOptionName = "quieter"
silentOptionName = "silent"
progressOptionName = "progress"
trickleOptionName = "trickle"
Expand Down Expand Up @@ -73,6 +74,7 @@ You can now refer to the added file in a gateway, like so:
Options: []cmds.Option{
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
cmds.BoolOption(quietOptionName, "q", "Write minimal output."),
cmds.BoolOption(quieterOptionName, "Q", "Write only final hash."),
cmds.BoolOption(silentOptionName, "Write no output."),
cmds.BoolOption(progressOptionName, "p", "Stream progress data."),
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
Expand All @@ -87,6 +89,9 @@ You can now refer to the added file in a gateway, like so:
},
PreRun: func(req cmds.Request) error {
quiet, _, _ := req.Option(quietOptionName).Bool()
quieter, _, _ := req.Option(quieterOptionName).Bool()
quiet = quiet || quieter

silent, _, _ := req.Option(silentOptionName).Bool()

if quiet || silent {
Expand Down Expand Up @@ -278,17 +283,11 @@ You can now refer to the added file in a gateway, like so:
}
res.SetOutput(nil)

quiet, _, err := req.Option("quiet").Bool()
if err != nil {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
quiet, _, _ := req.Option(quietOptionName).Bool()
quieter, _, _ := req.Option(quieterOptionName).Bool()
quiet = quiet || quieter

progress, _, err := req.Option(progressOptionName).Bool()
if err != nil {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
progress, _, _ := req.Option(progressOptionName).Bool()

var bar *pb.ProgressBar
if progress {
Expand All @@ -307,17 +306,26 @@ You can now refer to the added file in a gateway, like so:
}

lastFile := ""
lastHash := ""
var totalProgress, prevFiles, lastBytes int64

LOOP:
for {
select {
case out, ok := <-outChan:
if !ok {
if quieter {
fmt.Fprintln(res.Stdout(), lastHash)
}
break LOOP
}
output := out.(*coreunix.AddedObject)
if len(output.Hash) > 0 {
lastHash = output.Hash
if quieter {
continue
}

if progress {
// clear progress bar line before we print "added x" output
fmt.Fprintf(res.Stderr(), "\033[2K\r")
Expand All @@ -327,7 +335,6 @@ You can now refer to the added file in a gateway, like so:
} else {
fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
}

} else {
log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)

Expand Down
9 changes: 9 additions & 0 deletions test/sharness/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ add_directory() {
test_cmp expected actual
'

test_expect_success "ipfs add --quieter succeeds" '
ipfs add -r -Q $EXTRA_ARGS mountdir/planets >actual
'

test_expect_success "ipfs add --quieter returns only one correct hash" '
echo "$PLANETS" > expected &&
test_cmp expected actual
'

test_expect_success "cleanup" '
rm -r mountdir/planets
'
Expand Down