return history properly sorted

This commit is contained in:
anianz
2020-11-11 12:55:48 +01:00
parent cbbfd94fbb
commit df1fd83256
3 changed files with 15 additions and 5 deletions

View File

@@ -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)