mirror of
https://github.com/kopia/kopia.git
synced 2025-12-23 22:57:50 -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
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/internal/acl"
|
|
"github.com/kopia/kopia/repo"
|
|
)
|
|
|
|
type commandACLDelete struct {
|
|
ids []string
|
|
all bool
|
|
confirm bool
|
|
}
|
|
|
|
func (c *commandACLDelete) setup(svc appServices, parent commandParent) {
|
|
cmd := parent.Command("delete", "Delete ACL entry").Alias("remove").Alias("rm")
|
|
cmd.Arg("id", "Entry ID").StringsVar(&c.ids)
|
|
cmd.Flag("all", "Remove all ACL entries").BoolVar(&c.all)
|
|
cmd.Flag("delete", "Really delete").BoolVar(&c.confirm)
|
|
cmd.Action(svc.repositoryWriterAction(c.run))
|
|
}
|
|
|
|
func dryRunDelete(ctx context.Context, e *acl.Entry) {
|
|
log(ctx).Infof("would delete entry %v, pass --delete to actually delete", e.ManifestID)
|
|
}
|
|
|
|
func (c *commandACLDelete) shouldRemoveACLEntry(ctx context.Context, e *acl.Entry) bool {
|
|
if c.all {
|
|
if !c.confirm {
|
|
dryRunDelete(ctx, e)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
for _, tr := range c.ids {
|
|
if tr == string(e.ManifestID) {
|
|
if !c.confirm {
|
|
dryRunDelete(ctx, e)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (c *commandACLDelete) run(ctx context.Context, rep repo.RepositoryWriter) error {
|
|
entries, err := acl.LoadEntries(ctx, rep, nil)
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to load entries")
|
|
}
|
|
|
|
for _, e := range entries {
|
|
if c.shouldRemoveACLEntry(ctx, e) {
|
|
if err := rep.DeleteManifest(ctx, e.ManifestID); err != nil {
|
|
return errors.Wrap(err, "unable to delete manifest")
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|