2017-09-10 16:13:05 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-09-13 23:27:18 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-09-10 16:13:05 +02:00
|
|
|
"github.com/zrepl/zrepl/zfs"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-11-12 23:02:23 +01:00
|
|
|
type PrefixFilter struct {
|
|
|
|
prefix string
|
|
|
|
fstype zfs.VersionType
|
|
|
|
fstypeSet bool // optionals anyone?
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPrefixFilter(prefix string) *PrefixFilter {
|
|
|
|
return &PrefixFilter{prefix: prefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTypedPrefixFilter(prefix string, versionType zfs.VersionType) *PrefixFilter {
|
|
|
|
return &PrefixFilter{prefix, versionType, true}
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
func parseSnapshotPrefix(i string) (p string, err error) {
|
|
|
|
if len(i) <= 0 {
|
|
|
|
err = errors.Errorf("snapshot prefix must not be empty string")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p = i
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:20:37 +02:00
|
|
|
func (f *PrefixFilter) Filter(t zfs.VersionType, name string) (accept bool, err error) {
|
|
|
|
fstypeMatches := (!f.fstypeSet || t == f.fstype)
|
|
|
|
prefixMatches := strings.HasPrefix(name, f.prefix)
|
2017-11-12 23:02:23 +01:00
|
|
|
return fstypeMatches && prefixMatches, nil
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|