Files
kopia/cli/command_blob_gc.go
Jarek Kowalski e03971fc59 Upgraded linter to v1.33.0 (#734)
* linter: upgraded to 1.33, disabled some linters

* lint: fixed 'errorlint' errors

This ensures that all error comparisons use errors.Is() or errors.As().
We will be wrapping more errors going forward so it's important that
error checks are not strict everywhere.

Verified that there are no exceptions for errorlint linter which
guarantees that.

* lint: fixed or suppressed wrapcheck errors

* lint: nolintlint and misc cleanups

Co-authored-by: Julio López <julio+gh@kasten.io>
2020-12-21 22:39:22 -08:00

46 lines
1.5 KiB
Go

package cli
import (
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/maintenance"
)
var (
blobGarbageCollectCommand = blobCommands.Command("gc", "Garbage-collect unused blobs")
blobGarbageCollectCommandDelete = blobGarbageCollectCommand.Flag("delete", "Whether to delete unused blobs").String()
blobGarbageCollectParallel = blobGarbageCollectCommand.Flag("parallel", "Number of parallel blob scans").Default("16").Int()
blobGarbageCollectMinAge = blobGarbageCollectCommand.Flag("min-age", "Garbage-collect blobs with minimum age").Default("24h").Duration()
blobGarbageCollectPrefix = blobGarbageCollectCommand.Flag("prefix", "Only GC blobs with given prefix").String()
)
func runBlobGarbageCollectCommand(ctx context.Context, rep *repo.DirectRepository) error {
advancedCommand(ctx)
opts := maintenance.DeleteUnreferencedBlobsOptions{
DryRun: *blobGarbageCollectCommandDelete != "yes",
MinAge: *blobGarbageCollectMinAge,
Parallel: *blobGarbageCollectParallel,
Prefix: blob.ID(*blobGarbageCollectPrefix),
}
n, err := maintenance.DeleteUnreferencedBlobs(ctx, rep, opts)
if err != nil {
return errors.Wrap(err, "error deleting unreferenced blobs")
}
if opts.DryRun && n > 0 {
log(ctx).Infof("Pass --delete=yes to delete.")
}
return nil
}
func init() {
blobGarbageCollectCommand.Action(directRepositoryAction(runBlobGarbageCollectCommand))
}