mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
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>
This commit is contained in:
parent
9f6cd22ba4
commit
bfa9d3db99
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user