Sorting the job summaries while displaying

This commit is contained in:
Diptanu Choudhury 2016-08-04 10:42:53 -07:00
parent d73d708a98
commit 5d3a2ac1e5
2 changed files with 31 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/gob"
"fmt"
"sort"
"strings"
"time"
@ -266,6 +267,7 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
)
idx += 1
}
sort.Sort(JobSummaryOutputSort(summaries[1:]))
c.Ui.Output(formatList(summaries))
}
@ -374,3 +376,21 @@ func convertApiJob(in *api.Job) (*structs.Job, error) {
}
return structJob, nil
}
// JobSummaryOutputSort implements the sort interface and sorts an array of job
// summary output
type JobSummaryOutputSort []string
func (j JobSummaryOutputSort) Len() int {
return len(j)
}
func (j JobSummaryOutputSort) Less(a, b int) bool {
taskGroupA := j[a][:strings.Index(j[a], "|")]
taskGroupB := j[b][:strings.Index(j[b], "|")]
return taskGroupA < taskGroupB
}
func (j JobSummaryOutputSort) Swap(a, b int) {
j[a], j[b] = j[b], j[a]
}

View File

@ -1,6 +1,8 @@
package command
import (
"reflect"
"sort"
"strings"
"testing"
@ -170,3 +172,12 @@ func TestStatusCommand_Fails(t *testing.T) {
t.Fatalf("expected failed query error, got: %s", out)
}
}
func TestStatusCommand_SortTG(t *testing.T) {
summary := []string{"TGD|3|5|6|7", "TGA|3|5|6|7", "TGC|3|5|6|7"}
sort.Sort(JobSummaryOutputSort(summary))
expected := []string{"TGA|3|5|6|7", "TGC|3|5|6|7", "TGD|3|5|6|7"}
if !reflect.DeepEqual(expected, summary) {
t.Fatalf("expected: %v, actual: %v", expected, summary)
}
}