From df1fd832561d447c96f67f38e830980bcf9f7969 Mon Sep 17 00:00:00 2001 From: anianz Date: Wed, 11 Nov 2020 12:55:48 +0100 Subject: [PATCH] return history properly sorted --- check/main.go | 12 ++++++++---- common/common.go | 2 +- models.go | 6 ++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/check/main.go b/check/main.go index b3e133f..31e2bcb 100644 --- a/check/main.go +++ b/check/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "os" + "sort" resource "github.com/cioplenu/concourse-nomad-resource" "github.com/cioplenu/concourse-nomad-resource/common" @@ -21,16 +22,19 @@ func main() { lastVersion := request.Version.Version history := common.GetHistory(request.Source) + sort.Sort(history) // Nomad provides history from newest to oldest versions := make([]resource.Version, 0) - for _, jobVersion := range history { + for i, jobVersion := range history { + // Only return the current version and newer ones if lastVersion != 0 && lastVersion > jobVersion.Version { continue } - versions = append(versions, resource.Version{jobVersion.Version}) - if lastVersion == 0 { - break + // If no version was provided return only the latest + if lastVersion == 0 && i < len(history)-1 { + continue } + versions = append(versions, resource.Version{jobVersion.Version}) } json.NewEncoder(os.Stdout).Encode(versions) diff --git a/common/common.go b/common/common.go index 842e98d..eeec823 100644 --- a/common/common.go +++ b/common/common.go @@ -17,7 +17,7 @@ func Check(err error, msg string) { } } -func GetHistory(source resource.Source) []resource.JobVersion { +func GetHistory(source resource.Source) resource.History { cmd := exec.Command( "nomad", "job", diff --git a/models.go b/models.go index 2206e82..4450af9 100644 --- a/models.go +++ b/models.go @@ -12,6 +12,12 @@ type JobVersion struct { SubmitTime int } +type History []JobVersion + +func (h History) Len() int { return len(h) } +func (h History) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h History) Less(i, j int) bool { return h[i].Version < h[j].Version } + type Version struct { Version int `json:"Version,string"` }