Skip to content

Commit

Permalink
Move common code to common package
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jun 10, 2024
1 parent 8c62d5a commit d70edb7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
25 changes: 25 additions & 0 deletions cmd/internal/backup/providers/common/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package common

import (
"fmt"
"slices"

"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers"
)

type backupVersionLister interface {
List() []*providers.BackupVersion
}

// Sort the given list of backup versions
func Sort(versions []*providers.BackupVersion, oldestFirst bool) {
slices.SortFunc(versions, func(a, b *providers.BackupVersion) int {
if oldestFirst {
Expand All @@ -14,3 +20,22 @@ func Sort(versions []*providers.BackupVersion, oldestFirst bool) {
return a.Date.Compare(b.Date)
})
}

// Latest returns latest backup version
func Latest(versions []*providers.BackupVersion) *providers.BackupVersion {
Sort(versions, true)
if len(versions) == 0 {
return nil
}
return versions[0]
}

// Latest returns the backup version at given version
func Get(b backupVersionLister, 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)
}
29 changes: 29 additions & 0 deletions cmd/internal/backup/providers/common/common_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -60,3 +61,31 @@ func TestSort(t *testing.T) {
})
}
}

func TestLatest(t *testing.T) {
now := time.Now()
newestBackup := &providers.BackupVersion{Name: "5.tgz", Date: now.Add(5 * time.Hour)}
tests := []struct {
name string
versions []*providers.BackupVersion
want *providers.BackupVersion
}{
{
versions: []*providers.BackupVersion{
{Name: "0.tgz", Date: now},
{Name: "2.tgz", Date: now.Add(2 * time.Hour)},
{Name: "1.tgz", Date: now.Add(1 * time.Hour)},
newestBackup,
{Name: "3.tgz", Date: now.Add(3 * time.Hour)},
},
want: newestBackup,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Latest(tt.versions); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Latest() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 3 additions & 13 deletions cmd/internal/backup/providers/gcp/versions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gcp

import (
"fmt"
"strconv"

"cloud.google.com/go/storage"
Expand All @@ -14,17 +13,13 @@ type backupVersionsGCP struct {
}

func (b backupVersionsGCP) Latest() *providers.BackupVersion {
result := b.List()
if len(result) == 0 {
return nil
}
return result[0]
return common.Latest(b.List())
}

func (b backupVersionsGCP) List() []*providers.BackupVersion {
var result []*providers.BackupVersion

tmp := make(map[int64]bool)
tmp := make(map[int64]bool, len(result))
for _, attr := range b.objectAttrs {
ok := tmp[attr.Generation]
if !ok {
Expand All @@ -43,10 +38,5 @@ func (b backupVersionsGCP) List() []*providers.BackupVersion {
}

func (b backupVersionsGCP) 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)
return common.Get(b, version)
}
15 changes: 2 additions & 13 deletions cmd/internal/backup/providers/local/versions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package local

import (
"fmt"
"os"
"strconv"

Expand All @@ -14,12 +13,7 @@ type backupVersionsLocal struct {
}

func (b backupVersionsLocal) Latest() *providers.BackupVersion {
result := b.List()
if len(result) == 0 {
return nil
}
common.Sort(result, false)
return result[0]
return common.Latest(b.List())
}

func (b backupVersionsLocal) List() []*providers.BackupVersion {
Expand All @@ -42,10 +36,5 @@ func (b backupVersionsLocal) List() []*providers.BackupVersion {
}

func (b backupVersionsLocal) 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)
return common.Get(b, version)
}
15 changes: 2 additions & 13 deletions cmd/internal/backup/providers/s3/versions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package s3

import (
"fmt"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/backup/providers/common"
Expand All @@ -15,11 +13,7 @@ type backupVersionsS3 struct {

// Latest returns latest backup version
func (b backupVersionsS3) Latest() *providers.BackupVersion {
result := b.List()
if len(result) == 0 {
return nil
}
return result[0]
return common.Latest(b.List())
}

// List return a list of all backup versions
Expand All @@ -41,10 +35,5 @@ func (b backupVersionsS3) List() []*providers.BackupVersion {

// 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)
return common.Get(b, version)
}

0 comments on commit d70edb7

Please sign in to comment.