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

51 lines
2.1 KiB
Go

package cli
import (
"context"
"github.com/alecthomas/kingpin"
"github.com/kopia/kopia/snapshot/policy"
)
type policyRetentionFlags struct {
policySetKeepLatest string
policySetKeepHourly string
policySetKeepDaily string
policySetKeepWeekly string
policySetKeepMonthly string
policySetKeepAnnual string
}
func (c *policyRetentionFlags) setup(cmd *kingpin.CmdClause) {
cmd.Flag("keep-latest", "Number of most recent backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepLatest)
cmd.Flag("keep-hourly", "Number of most-recent hourly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepHourly)
cmd.Flag("keep-daily", "Number of most-recent daily backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepDaily)
cmd.Flag("keep-weekly", "Number of most-recent weekly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepWeekly)
cmd.Flag("keep-monthly", "Number of most-recent monthly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepMonthly)
cmd.Flag("keep-annual", "Number of most-recent annual backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepAnnual)
}
func (c *policyRetentionFlags) setRetentionPolicyFromFlags(ctx context.Context, rp *policy.RetentionPolicy, changeCount *int) error {
cases := []struct {
desc string
max **int
flagValue string
}{
{"number of annual backups to keep", &rp.KeepAnnual, c.policySetKeepAnnual},
{"number of monthly backups to keep", &rp.KeepMonthly, c.policySetKeepMonthly},
{"number of weekly backups to keep", &rp.KeepWeekly, c.policySetKeepWeekly},
{"number of daily backups to keep", &rp.KeepDaily, c.policySetKeepDaily},
{"number of hourly backups to keep", &rp.KeepHourly, c.policySetKeepHourly},
{"number of latest backups to keep", &rp.KeepLatest, c.policySetKeepLatest},
}
for _, c := range cases {
if err := applyPolicyNumber(ctx, c.desc, c.max, c.flagValue, changeCount); err != nil {
return err
}
}
return nil
}