From b2742dc28d1ff9001cd784455bbdf9cf29539c30 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Thu, 14 Sep 2017 07:42:50 +0200 Subject: Implement remaining subcommands --- storage/storage.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'storage') diff --git a/storage/storage.go b/storage/storage.go index 7cffe9c..7d09a70 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "time" ) var ( @@ -89,3 +90,39 @@ func GetObjectOfType(s Storage, id objects.ObjectId, t objects.ObjectType) (obje return rawobj.Object() } + +// FindLatestSnapshot finds the latest snapshot, optionally filtered by archive +func FindLatestSnapshot(store Storage, archive string) (latestSnapshot *objects.Snapshot, err error) { + ids, err := store.List(objects.OTSnapshot) + if err != nil { + return nil, err + } + + var earliestTime time.Time + found := false + + for _, id := range ids { + _snapshot, err := GetObjectOfType(store, id, objects.OTSnapshot) + if err != nil { + return nil, err + } + + snapshot := _snapshot.(*objects.Snapshot) + + if archive != "" && snapshot.Archive != archive { + continue + } + + if snapshot.Date.After(earliestTime) { + earliestTime = snapshot.Date + latestSnapshot = snapshot + found = true + } + } + + if !found { + return latestSnapshot, ObjectNotFound + } + + return latestSnapshot, nil +} -- cgit v1.2.3-70-g09d2