return history properly sorted
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
|
||||||
resource "github.com/cioplenu/concourse-nomad-resource"
|
resource "github.com/cioplenu/concourse-nomad-resource"
|
||||||
"github.com/cioplenu/concourse-nomad-resource/common"
|
"github.com/cioplenu/concourse-nomad-resource/common"
|
||||||
@@ -21,16 +22,19 @@ func main() {
|
|||||||
lastVersion := request.Version.Version
|
lastVersion := request.Version.Version
|
||||||
|
|
||||||
history := common.GetHistory(request.Source)
|
history := common.GetHistory(request.Source)
|
||||||
|
sort.Sort(history) // Nomad provides history from newest to oldest
|
||||||
versions := make([]resource.Version, 0)
|
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 {
|
if lastVersion != 0 && lastVersion > jobVersion.Version {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
versions = append(versions, resource.Version{jobVersion.Version})
|
// If no version was provided return only the latest
|
||||||
if lastVersion == 0 {
|
if lastVersion == 0 && i < len(history)-1 {
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
versions = append(versions, resource.Version{jobVersion.Version})
|
||||||
}
|
}
|
||||||
|
|
||||||
json.NewEncoder(os.Stdout).Encode(versions)
|
json.NewEncoder(os.Stdout).Encode(versions)
|
||||||
|
@@ -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(
|
cmd := exec.Command(
|
||||||
"nomad",
|
"nomad",
|
||||||
"job",
|
"job",
|
||||||
|
@@ -12,6 +12,12 @@ type JobVersion struct {
|
|||||||
SubmitTime int
|
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 {
|
type Version struct {
|
||||||
Version int `json:"Version,string"`
|
Version int `json:"Version,string"`
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user