-
Notifications
You must be signed in to change notification settings - Fork 1
/
versions.go
60 lines (50 loc) · 1.4 KB
/
versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package s3
import (
"fmt"
"sort"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers"
)
// BackupVersionsS3 contains the list of available backup versions
type BackupVersionsS3 struct {
objectAttrs []*s3.ObjectVersion
}
// Latest returns latest backup version
func (b BackupVersionsS3) Latest() *providers.BackupVersion {
result := b.List()
if len(result) == 0 {
return nil
}
return result[0]
}
// List return a list of all backup versions
func (b BackupVersionsS3) List() []*providers.BackupVersion {
var result []*providers.BackupVersion
for _, attr := range b.objectAttrs {
result = append(result, &providers.BackupVersion{
Name: *attr.Key,
Version: *attr.VersionId,
Date: *attr.LastModified,
})
}
b.Sort(result, false)
return result
}
// Sort returns the backup versions sorted by date
func (b BackupVersionsS3) Sort(versions []*providers.BackupVersion, asc bool) {
sort.Slice(versions, func(i, j int) bool {
if asc {
return versions[i].Date.Before(versions[j].Date)
}
return versions[i].Date.After(versions[j].Date)
})
}
// Get returns the backup entry of the given version
func (b BackupVersionsS3) Get(version string) (*providers.BackupVersion, error) {
for _, backup := range b.List() {
if version == backup.Version {
return backup, nil
}
}
return nil, fmt.Errorf("version %q not found", version)
}