diff --git a/command/status.go b/command/status.go index 825d805c4..89143cb86 100644 --- a/command/status.go +++ b/command/status.go @@ -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] +} diff --git a/command/status_test.go b/command/status_test.go index 270d91357..dc422014d 100644 --- a/command/status_test.go +++ b/command/status_test.go @@ -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) + } +}