Files
kopia/cli/command_blob_gc.go
Jarek Kowalski d2288c443f cli: major refactoring (#1046)
cli: major refactoring of how CLI commands are registered

The goal is to eliminate flags as global variables to allow for better
testing. Each command and subcommand and most sets of flags are now
their own struct with 'setup()' methods that attached the flags or
subcommand to the provided parent.

This change is 94.3% mechanical, but is fully organic and hand-made.

* introduced cli.appServices interface which provides the environment in which commands run
* remove auto-maintenance global flag
* removed globals in memory_tracking.go
* removed globals from cli_progress.go
* removed globals from the update_check.go
* moved configPath into TheApp
* removed remaining globals from config.go
* refactored logfile to get rid of global variables
* removed 'app' global variable
* linter fixes
* fixed password_*.go build
* fixed BSD build
2021-05-03 10:28:00 -07:00

49 lines
1.2 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"
)
type commandBlobGC struct {
delete string
parallel int
prefix string
safety maintenance.SafetyParameters
}
func (c *commandBlobGC) setup(svc appServices, parent commandParent) {
cmd := parent.Command("gc", "Garbage-collect unused blobs")
cmd.Flag("delete", "Whether to delete unused blobs").StringVar(&c.delete)
cmd.Flag("parallel", "Number of parallel blob scans").Default("16").IntVar(&c.parallel)
cmd.Flag("prefix", "Only GC blobs with given prefix").StringVar(&c.prefix)
safetyFlagVar(cmd, &c.safety)
cmd.Action(svc.directRepositoryWriteAction(c.run))
}
func (c *commandBlobGC) run(ctx context.Context, rep repo.DirectRepositoryWriter) error {
advancedCommand(ctx)
opts := maintenance.DeleteUnreferencedBlobsOptions{
DryRun: c.delete != "yes",
Parallel: c.parallel,
Prefix: blob.ID(c.prefix),
}
n, err := maintenance.DeleteUnreferencedBlobs(ctx, rep, opts, c.safety)
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
}