pruning: add 'Negate' option to KeepRegex and expose it in config

This commit is contained in:
Christian Schwarz
2018-11-16 11:32:24 +01:00
parent 2db3977408
commit 5e1ea21f85
7 changed files with 82 additions and 15 deletions

View File

@ -6,20 +6,21 @@ import (
type KeepRegex struct {
expr *regexp.Regexp
negate bool
}
var _ KeepRule = &KeepRegex{}
func NewKeepRegex(expr string) (*KeepRegex, error) {
func NewKeepRegex(expr string, negate bool) (*KeepRegex, error) {
re, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return &KeepRegex{re}, nil
return &KeepRegex{re, negate}, nil
}
func MustKeepRegex(expr string) *KeepRegex {
k, err := NewKeepRegex(expr)
func MustKeepRegex(expr string, negate bool) *KeepRegex {
k, err := NewKeepRegex(expr, negate)
if err != nil {
panic(err)
}
@ -28,6 +29,10 @@ func MustKeepRegex(expr string) *KeepRegex {
func (k *KeepRegex) KeepRule(snaps []Snapshot) []Snapshot {
return filterSnapList(snaps, func(s Snapshot) bool {
return k.expr.FindStringIndex(s.Name()) == nil
if k.negate {
return k.expr.FindStringIndex(s.Name()) != nil
} else {
return k.expr.FindStringIndex(s.Name()) == nil
}
})
}