From 70ae8d2736732511b2c01b694bd6407f680fe83e Mon Sep 17 00:00:00 2001 From: Norberto Lopes Date: Mon, 11 Sep 2017 09:05:09 +0100 Subject: [PATCH] Sort images by UpdatedAt before finding latest --- client/client.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/client/client.go b/client/client.go index b898957a..e41e140d 100644 --- a/client/client.go +++ b/client/client.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "reflect" + "sort" "strconv" "strings" @@ -79,11 +80,15 @@ func (c Client) GetLatestImage() (models.Image, error) { return image, err } - if len(images) > 0 { - for i := len(images) - 1; i >= 0; i-- { - if images[i].Ready { - return images[i], nil - } + // Make sure they are all sorted by UpdatedAt + // The most up to date should be the first of the slice + sort.Slice(images, func(i, j int) bool { + return images[i].UpdatedAt.After(images[j].UpdatedAt) + }) + + for _, image := range images { + if image.Ready { + return image, nil } }