From bfa9d3db997f6e38055ecf2db8a5ac111d231ebd Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 18 Feb 2022 13:29:32 -0800 Subject: [PATCH] feat(cmd): add silent option for repo gc (#7147) * feat(cmd): add silent option repo gc command closes #7129 * test(cmd): add test case for silent option for command repo gc * fix: no emit on server with --silent This removes unnecessary send to the client that does not care Co-authored-by: Marcin Rataj --- core/commands/repo.go | 11 +++++++++++ test/sharness/t0080-repo.sh | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/commands/repo.go b/core/commands/repo.go index 20cc41fcd..c4066f50e 100644 --- a/core/commands/repo.go +++ b/core/commands/repo.go @@ -51,6 +51,7 @@ type GcResult struct { const ( repoStreamErrorsOptionName = "stream-errors" repoQuietOptionName = "quiet" + repoSilentOptionName = "silent" ) var repoGcCmd = &cmds.Command{ @@ -65,6 +66,7 @@ order to reclaim hard disk space. Options: []cmds.Option{ cmds.BoolOption(repoStreamErrorsOptionName, "Stream errors."), cmds.BoolOption(repoQuietOptionName, "q", "Write minimal output."), + cmds.BoolOption(repoSilentOptionName, "Write no output."), }, Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error { n, err := cmdenv.GetNode(env) @@ -72,6 +74,7 @@ order to reclaim hard disk space. return err } + silent, _ := req.Options[repoSilentOptionName].(bool) streamErrors, _ := req.Options[repoStreamErrorsOptionName].(bool) gcOutChan := corerepo.GarbageCollectAsync(n, req.Context) @@ -95,6 +98,9 @@ order to reclaim hard disk space. } } else { err := corerepo.CollectResult(req.Context, gcOutChan, func(k cid.Cid) { + if silent { + return + } // Nothing to do with this error, really. This // most likely means that the client is gone but // we still need to let the GC finish. @@ -111,6 +117,11 @@ order to reclaim hard disk space. Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, gcr *GcResult) error { quiet, _ := req.Options[repoQuietOptionName].(bool) + silent, _ := req.Options[repoSilentOptionName].(bool) + + if silent { + return nil + } if gcr.Error != "" { _, err := fmt.Fprintf(w, "Error: %s\n", gcr.Error) diff --git a/test/sharness/t0080-repo.sh b/test/sharness/t0080-repo.sh index a6631b53c..839e651d1 100755 --- a/test/sharness/t0080-repo.sh +++ b/test/sharness/t0080-repo.sh @@ -55,6 +55,17 @@ test_expect_success "ipfs repo gc fully reverse ipfs add (part 1)" ' ipfs pin rm -r $hash && ipfs repo gc ' +test_expect_success "'ipfs repo gc --silent' succeeds (no output)" ' + echo "should be empty" >bfile && + HASH2=`ipfs add -q bfile` && + ipfs cat "$HASH2" >expected11 && + test_cmp expected11 bfile && + ipfs pin rm -r "$HASH2" && + ipfs repo gc --silent >gc_out_empty && + test_cmp /dev/null gc_out_empty && + test_must_fail ipfs cat "$HASH2" 2>err_expected1 && + grep "Error: merkledag: not found" err_expected1 +' test_kill_ipfs_daemon