-
Notifications
You must be signed in to change notification settings - Fork 379
/
command_logs_cleanup.go
53 lines (43 loc) · 1.38 KB
/
command_logs_cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cli
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/maintenance"
)
type commandLogsCleanup struct {
maxTotalSizeMB int64
maxCount int
maxAge time.Duration
dryRun bool
}
func (c *commandLogsCleanup) setup(svc appServices, parent commandParent) {
cmd := parent.Command("cleanup", "Clean up logs")
cmd.Flag("max-age", "Maximal age").Default("720h").DurationVar(&c.maxAge)
cmd.Flag("max-count", "Maximal number of files to keep").Default("10000").IntVar(&c.maxCount)
cmd.Flag("max-total-size-mb", "Maximal total size in MiB").Default("1024").Int64Var(&c.maxTotalSizeMB)
cmd.Flag("dry-run", "Do not delete").BoolVar(&c.dryRun)
cmd.Action(svc.directRepositoryWriteAction(c.run))
}
func (c *commandLogsCleanup) run(ctx context.Context, rep repo.DirectRepositoryWriter) error {
toDelete, err := maintenance.CleanupLogs(ctx, rep, maintenance.LogRetentionOptions{
MaxTotalSize: c.maxTotalSizeMB << 20, //nolint:gomnd
MaxCount: c.maxCount,
MaxAge: c.maxAge,
DryRun: c.dryRun,
})
if err != nil {
return errors.Wrap(err, "error expiring logs")
}
if len(toDelete) > 0 {
if c.dryRun {
log(ctx).Infof("Would delete %v logs.", len(toDelete))
} else {
log(ctx).Infof("Deleted %v logs.", len(toDelete))
}
} else {
log(ctx).Infof("No logs found to delete.")
}
return nil
}