return history properly sorted
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user