Files
kopia/cli/command_policy_set_files.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

62 lines
2.7 KiB
Go

package cli
import (
"context"
"github.com/alecthomas/kingpin"
"github.com/pkg/errors"
"github.com/kopia/kopia/snapshot/policy"
)
type policyFilesFlags struct {
// Ignore rules.
policySetAddIgnore []string
policySetRemoveIgnore []string
policySetClearIgnore bool
// Dot-ignore files to look at.
policySetAddDotIgnore []string
policySetRemoveDotIgnore []string
policySetClearDotIgnore bool
policySetMaxFileSize string
// Ignore other mounted fileystems.
policyOneFileSystem string
policyIgnoreCacheDirs string
}
func (c *policyFilesFlags) setup(cmd *kingpin.CmdClause) {
// Ignore rules.
cmd.Flag("add-ignore", "List of paths to add to the ignore list").PlaceHolder("PATTERN").StringsVar(&c.policySetAddIgnore)
cmd.Flag("remove-ignore", "List of paths to remove from the ignore list").PlaceHolder("PATTERN").StringsVar(&c.policySetRemoveIgnore)
cmd.Flag("clear-ignore", "Clear list of paths in the ignore list").BoolVar(&c.policySetClearIgnore)
// Dot-ignore files to look at.
cmd.Flag("add-dot-ignore", "List of paths to add to the dot-ignore list").PlaceHolder("FILENAME").StringsVar(&c.policySetAddDotIgnore)
cmd.Flag("remove-dot-ignore", "List of paths to remove from the dot-ignore list").PlaceHolder("FILENAME").StringsVar(&c.policySetRemoveDotIgnore)
cmd.Flag("clear-dot-ignore", "Clear list of paths in the dot-ignore list").BoolVar(&c.policySetClearDotIgnore)
cmd.Flag("max-file-size", "Exclude files above given size").PlaceHolder("N").StringVar(&c.policySetMaxFileSize)
// Ignore other mounted fileystems.
cmd.Flag("one-file-system", "Stay in parent filesystem when finding files ('true', 'false', 'inherit')").EnumVar(&c.policyOneFileSystem, booleanEnumValues...)
cmd.Flag("ignore-cache-dirs", "Ignore cache directories ('true', 'false', 'inherit')").EnumVar(&c.policyIgnoreCacheDirs, booleanEnumValues...)
}
func (c *policyFilesFlags) setFilesPolicyFromFlags(ctx context.Context, fp *policy.FilesPolicy, changeCount *int) error {
if err := applyPolicyNumber64(ctx, "maximum file size", &fp.MaxFileSize, c.policySetMaxFileSize, changeCount); err != nil {
return errors.Wrap(err, "maximum file size")
}
applyPolicyStringList(ctx, "dot-ignore filenames", &fp.DotIgnoreFiles, c.policySetAddDotIgnore, c.policySetRemoveDotIgnore, c.policySetClearDotIgnore, changeCount)
applyPolicyStringList(ctx, "ignore rules", &fp.IgnoreRules, c.policySetAddIgnore, c.policySetRemoveIgnore, c.policySetClearIgnore, changeCount)
if err := applyPolicyBoolPtr(ctx, "ignore cache dirs", &fp.IgnoreCacheDirs, c.policyIgnoreCacheDirs, changeCount); err != nil {
return err
}
return applyPolicyBoolPtr(ctx, "one filesystem", &fp.OneFileSystem, c.policyOneFileSystem, changeCount)
}