Skip to content

Commit bfa9d3d

Browse files
will7200lidel
andauthored
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>
1 parent 9f6cd22 commit bfa9d3d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

core/commands/repo.go

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type GcResult struct {
5151
const (
5252
repoStreamErrorsOptionName = "stream-errors"
5353
repoQuietOptionName = "quiet"
54+
repoSilentOptionName = "silent"
5455
)
5556

5657
var repoGcCmd = &cmds.Command{
@@ -65,13 +66,15 @@ order to reclaim hard disk space.
6566
Options: []cmds.Option{
6667
cmds.BoolOption(repoStreamErrorsOptionName, "Stream errors."),
6768
cmds.BoolOption(repoQuietOptionName, "q", "Write minimal output."),
69+
cmds.BoolOption(repoSilentOptionName, "Write no output."),
6870
},
6971
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
7072
n, err := cmdenv.GetNode(env)
7173
if err != nil {
7274
return err
7375
}
7476

77+
silent, _ := req.Options[repoSilentOptionName].(bool)
7578
streamErrors, _ := req.Options[repoStreamErrorsOptionName].(bool)
7679

7780
gcOutChan := corerepo.GarbageCollectAsync(n, req.Context)
@@ -95,6 +98,9 @@ order to reclaim hard disk space.
9598
}
9699
} else {
97100
err := corerepo.CollectResult(req.Context, gcOutChan, func(k cid.Cid) {
101+
if silent {
102+
return
103+
}
98104
// Nothing to do with this error, really. This
99105
// most likely means that the client is gone but
100106
// we still need to let the GC finish.
@@ -111,6 +117,11 @@ order to reclaim hard disk space.
111117
Encoders: cmds.EncoderMap{
112118
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, gcr *GcResult) error {
113119
quiet, _ := req.Options[repoQuietOptionName].(bool)
120+
silent, _ := req.Options[repoSilentOptionName].(bool)
121+
122+
if silent {
123+
return nil
124+
}
114125

115126
if gcr.Error != "" {
116127
_, err := fmt.Fprintf(w, "Error: %s\n", gcr.Error)

test/sharness/t0080-repo.sh

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ test_expect_success "ipfs repo gc fully reverse ipfs add (part 1)" '
5555
ipfs pin rm -r $hash &&
5656
ipfs repo gc
5757
'
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+
'
5869

5970
test_kill_ipfs_daemon
6071

0 commit comments

Comments
 (0)