2018-08-27 15:09:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-30 11:44:43 +02:00
|
|
|
func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) {
|
2018-08-27 15:09:24 +02:00
|
|
|
|
|
|
|
if k.n > len(snaps) {
|
2018-08-30 11:44:43 +02:00
|
|
|
return []Snapshot{}
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res := shallowCopySnapList(snaps)
|
|
|
|
|
|
|
|
sort.Slice(res, func(i, j int) bool {
|
|
|
|
return res[i].Date().After(res[j].Date())
|
|
|
|
})
|
|
|
|
|
2018-08-30 11:44:43 +02:00
|
|
|
return res[k.n:]
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|