@cryptotaxi247 / kubo / commits / bfa9d3db9

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 <lidel@lidel.org>

Will committed Feb 18, 2022 at 13:29 UTC bfa9d3db997f6e38055ecf2db8a5ac111d231ebd
2 files changed +22
core/commands/repo.go
+11
@@ -51,6 +51,7 @@ type GcResult struct {
51 const (
52 repoStreamErrorsOptionName = "stream-errors"
53 repoQuietOptionName = "quiet"
54 + repoSilentOptionName = "silent"
55 )
56
57 var repoGcCmd = &cmds.Command{
@@ -65,6 +66,7 @@ order to reclaim hard disk space.
66 Options: []cmds.Option{
67 cmds.BoolOption(repoStreamErrorsOptionName, "Stream errors."),
68 cmds.BoolOption(repoQuietOptionName, "q", "Write minimal output."),
69 + cmds.BoolOption(repoSilentOptionName, "Write no output."),
70 },
71 Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
72 n, err := cmdenv.GetNode(env)
@@ -72,6 +74,7 @@ order to reclaim hard disk space.
74 return err
75 }
76
77 + silent, _ := req.Options[repoSilentOptionName].(bool)
78 streamErrors, _ := req.Options[repoStreamErrorsOptionName].(bool)
79
80 gcOutChan := corerepo.GarbageCollectAsync(n, req.Context)
@@ -95,6 +98,9 @@ order to reclaim hard disk space.
98 }
99 } else {
100 err := corerepo.CollectResult(req.Context, gcOutChan, func(k cid.Cid) {
101 + if silent {
102 + return
103 + }
104 // Nothing to do with this error, really. This
105 // most likely means that the client is gone but
106 // we still need to let the GC finish.
@@ -111,6 +117,11 @@ order to reclaim hard disk space.
117 Encoders: cmds.EncoderMap{
118 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, gcr *GcResult) error {
119 quiet, _ := req.Options[repoQuietOptionName].(bool)
120 + silent, _ := req.Options[repoSilentOptionName].(bool)
121 +
122 + if silent {
123 + return nil
124 + }
125
126 if gcr.Error != "" {
127 _, err := fmt.Fprintf(w, "Error: %s\n", gcr.Error)
test/sharness/t0080-repo.sh
+11
@@ -55,6 +55,17 @@ test_expect_success "ipfs repo gc fully reverse ipfs add (part 1)" '
55 ipfs pin rm -r $hash &&
56 ipfs repo gc
57 '
58 +test_expect_success "'ipfs repo gc --silent' succeeds (no output)" '
59 + echo "should be empty" >bfile &&
60 + HASH2=`ipfs add -q bfile` &&
61 + ipfs cat "$HASH2" >expected11 &&
62 + test_cmp expected11 bfile &&
63 + ipfs pin rm -r "$HASH2" &&
64 + ipfs repo gc --silent >gc_out_empty &&
65 + test_cmp /dev/null gc_out_empty &&
66 + test_must_fail ipfs cat "$HASH2" 2>err_expected1 &&
67 + grep "Error: merkledag: not found" err_expected1
68 +'
69
70 test_kill_ipfs_daemon
71