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

feat(cmd): add silent option for command repo gc #7147

Merged
merged 3 commits into from
Feb 18, 2022
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
11 changes: 11 additions & 0 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type GcResult struct {
const (
repoStreamErrorsOptionName = "stream-errors"
repoQuietOptionName = "quiet"
repoSilentOptionName = "silent"
)

var repoGcCmd = &cmds.Command{
Expand All @@ -65,13 +66,15 @@ 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)
if err != nil {
return err
}

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

gcOutChan := corerepo.GarbageCollectAsync(n, req.Context)
Expand All @@ -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.
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/sharness/t0080-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down