2018-08-27 15:09:24 +02:00
|
|
|
package pruning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeepRegex struct {
|
|
|
|
expr *regexp.Regexp
|
2018-11-16 11:32:24 +01:00
|
|
|
negate bool
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ KeepRule = &KeepRegex{}
|
|
|
|
|
2018-11-16 11:32:24 +01:00
|
|
|
func NewKeepRegex(expr string, negate bool) (*KeepRegex, error) {
|
2018-08-27 15:09:24 +02:00
|
|
|
re, err := regexp.Compile(expr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-16 11:32:24 +01:00
|
|
|
return &KeepRegex{re, negate}, nil
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 11:32:24 +01:00
|
|
|
func MustKeepRegex(expr string, negate bool) *KeepRegex {
|
|
|
|
k, err := NewKeepRegex(expr, negate)
|
2018-08-27 15:09:24 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KeepRegex) KeepRule(snaps []Snapshot) []Snapshot {
|
|
|
|
return filterSnapList(snaps, func(s Snapshot) bool {
|
2018-11-16 11:32:24 +01:00
|
|
|
if k.negate {
|
|
|
|
return k.expr.FindStringIndex(s.Name()) != nil
|
|
|
|
} else {
|
|
|
|
return k.expr.FindStringIndex(s.Name()) == nil
|
|
|
|
}
|
2018-08-27 15:09:24 +02:00
|
|
|
})
|
|
|
|
}
|