mirror of
https://github.com/kopia/kopia.git
synced 2026-01-23 13:58:08 -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
75 lines
3.2 KiB
Go
75 lines
3.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/alecthomas/kingpin"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo/compression"
|
|
"github.com/kopia/kopia/snapshot/policy"
|
|
)
|
|
|
|
type policyCompressionFlags struct {
|
|
policySetCompressionAlgorithm string
|
|
policySetCompressionMinSize string
|
|
policySetCompressionMaxSize string
|
|
|
|
policySetAddOnlyCompress []string
|
|
policySetRemoveOnlyCompress []string
|
|
policySetClearOnlyCompress bool
|
|
|
|
policySetAddNeverCompress []string
|
|
policySetRemoveNeverCompress []string
|
|
policySetClearNeverCompress bool
|
|
}
|
|
|
|
func (c *policyCompressionFlags) setup(cmd *kingpin.CmdClause) {
|
|
// Name of compression algorithm.
|
|
cmd.Flag("compression", "Compression algorithm").EnumVar(&c.policySetCompressionAlgorithm, supportedCompressionAlgorithms()...)
|
|
cmd.Flag("compression-min-size", "Min size of file to attempt compression for").StringVar(&c.policySetCompressionMinSize)
|
|
cmd.Flag("compression-max-size", "Max size of file to attempt compression for").StringVar(&c.policySetCompressionMaxSize)
|
|
|
|
// Files to only compress.
|
|
cmd.Flag("add-only-compress", "List of extensions to add to the only-compress list").PlaceHolder("PATTERN").StringsVar(&c.policySetAddOnlyCompress)
|
|
cmd.Flag("remove-only-compress", "List of extensions to remove from the only-compress list").PlaceHolder("PATTERN").StringsVar(&c.policySetRemoveOnlyCompress)
|
|
cmd.Flag("clear-only-compress", "Clear list of extensions in the only-compress list").BoolVar(&c.policySetClearOnlyCompress)
|
|
|
|
// Files to never compress.
|
|
cmd.Flag("add-never-compress", "List of extensions to add to the never compress list").PlaceHolder("PATTERN").StringsVar(&c.policySetAddNeverCompress)
|
|
cmd.Flag("remove-never-compress", "List of extensions to remove from the never compress list").PlaceHolder("PATTERN").StringsVar(&c.policySetRemoveNeverCompress)
|
|
cmd.Flag("clear-never-compress", "Clear list of extensions in the never compress list").BoolVar(&c.policySetClearNeverCompress)
|
|
}
|
|
|
|
func (c *policyCompressionFlags) setCompressionPolicyFromFlags(ctx context.Context, p *policy.CompressionPolicy, changeCount *int) error {
|
|
if err := applyPolicyNumber64(ctx, "minimum file size subject to compression", &p.MinSize, c.policySetCompressionMinSize, changeCount); err != nil {
|
|
return errors.Wrap(err, "minimum file size subject to compression")
|
|
}
|
|
|
|
if err := applyPolicyNumber64(ctx, "maximum file size subject to compression", &p.MaxSize, c.policySetCompressionMaxSize, changeCount); err != nil {
|
|
return errors.Wrap(err, "maximum file size subject to compression")
|
|
}
|
|
|
|
if v := c.policySetCompressionAlgorithm; v != "" {
|
|
*changeCount++
|
|
|
|
if v == inheritPolicyString {
|
|
log(ctx).Infof(" - resetting compression algorithm to default value inherited from parent\n")
|
|
|
|
p.CompressorName = ""
|
|
} else {
|
|
log(ctx).Infof(" - setting compression algorithm to %v\n", v)
|
|
|
|
p.CompressorName = compression.Name(v)
|
|
}
|
|
}
|
|
|
|
applyPolicyStringList(ctx, "only-compress extensions",
|
|
&p.OnlyCompress, c.policySetAddOnlyCompress, c.policySetRemoveOnlyCompress, c.policySetClearOnlyCompress, changeCount)
|
|
|
|
applyPolicyStringList(ctx, "never-compress extensions",
|
|
&p.NeverCompress, c.policySetAddNeverCompress, c.policySetRemoveNeverCompress, c.policySetClearNeverCompress, changeCount)
|
|
|
|
return nil
|
|
}
|