move cmd/pruning to pruning, as it's independent of the command implementation

This commit is contained in:
Christian Schwarz
2018-08-27 16:11:35 +02:00
parent fb0a8d8b40
commit 0de17fd051
9 changed files with 0 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
package pruning
import (
"github.com/pkg/errors"
"sort"
)
type KeepLastN struct {
n int
}
func NewKeepLastN(n int) (*KeepLastN, error) {
if n <= 0 {
return nil, errors.Errorf("must specify positive number as 'keep last count', got %d", n)
}
return &KeepLastN{n}, nil
}
func (k KeepLastN) KeepRule(snaps []Snapshot) []Snapshot {
if k.n > len(snaps) {
return snaps
}
res := shallowCopySnapList(snaps)
sort.Slice(res, func(i, j int) bool {
return res[i].Date().After(res[j].Date())
})
return res[:k.n]
}