mirror of
https://github.com/kopia/kopia.git
synced 2026-01-24 14:28:06 -05:00
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
36 lines
1.4 KiB
Go
36 lines
1.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/internal/units"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/maintenance"
|
|
"github.com/kopia/kopia/snapshot/snapshotgc"
|
|
)
|
|
|
|
type commandSnapshotGC struct {
|
|
snapshotGCDelete bool
|
|
snapshotGCSafety maintenance.SafetyParameters
|
|
}
|
|
|
|
func (c *commandSnapshotGC) setup(svc appServices, parent commandParent) {
|
|
cmd := parent.Command("gc", "Mark contents as deleted which are not used by any snapshot").Hidden()
|
|
cmd.Flag("delete", "Delete unreferenced contents").BoolVar(&c.snapshotGCDelete)
|
|
safetyFlagVar(cmd, &c.snapshotGCSafety)
|
|
cmd.Action(svc.directRepositoryWriteAction(c.run))
|
|
}
|
|
|
|
func (c *commandSnapshotGC) run(ctx context.Context, rep repo.DirectRepositoryWriter) error {
|
|
st, err := snapshotgc.Run(ctx, rep, c.snapshotGCDelete, c.snapshotGCSafety)
|
|
|
|
log(ctx).Infof("GC found %v unused contents (%v bytes)", st.UnusedCount, units.BytesStringBase2(st.UnusedBytes))
|
|
log(ctx).Infof("GC found %v unused contents that are too recent to delete (%v bytes)", st.TooRecentCount, units.BytesStringBase2(st.TooRecentBytes))
|
|
log(ctx).Infof("GC found %v in-use contents (%v bytes)", st.InUseCount, units.BytesStringBase2(st.InUseBytes))
|
|
log(ctx).Infof("GC found %v in-use system-contents (%v bytes)", st.SystemCount, units.BytesStringBase2(st.SystemBytes))
|
|
|
|
return errors.Wrap(err, "error running snapshot GC")
|
|
}
|