diff --git a/cmd/list/lister.go b/cmd/list/lister.go index 3a2752fe..769588c7 100644 --- a/cmd/list/lister.go +++ b/cmd/list/lister.go @@ -7,6 +7,7 @@ import ( "github.com/gojektech/proctor/daemon" "github.com/gojektech/proctor/io" "github.com/spf13/cobra" + "github.com/gojektech/proctor/utility/sort" ) func NewCmd(printer io.Printer, proctorDClient daemon.Client) *cobra.Command { @@ -21,8 +22,8 @@ func NewCmd(printer io.Printer, proctorDClient daemon.Client) *cobra.Command { printer.Println(err.Error(), color.FgRed) return } - printer.Println("List of Procs:\n", color.FgGreen) + sort.Procs(procList) for _, proc := range procList { printer.Println(fmt.Sprintf("%-40s %-100s", proc.Name, proc.Description), color.Reset) diff --git a/cmd/list/lister_test.go b/cmd/list/lister_test.go index 9982c457..8add6586 100644 --- a/cmd/list/lister_test.go +++ b/cmd/list/lister_test.go @@ -54,7 +54,6 @@ func (s *ListCmdTestSuite) TestListCmdRun() { s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procOne.Name, procOne.Description), color.Reset).Once() s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", procTwo.Name, procTwo.Description), color.Reset).Once() s.mockPrinter.On("Println", "\nFor detailed information of any proc, run:\nproctor describe ", color.FgGreen).Once() - s.testListCmd.Run(&cobra.Command{}, []string{}) s.mockProctorDClient.AssertExpectations(s.T()) diff --git a/utility/sort/sort.go b/utility/sort/sort.go new file mode 100644 index 00000000..cbc5cb97 --- /dev/null +++ b/utility/sort/sort.go @@ -0,0 +1,12 @@ +package sort + +import ( + "github.com/gojektech/proctor/proctord/jobs/metadata" + "sort" +) + +func Procs(procList []metadata.Metadata) { + sort.Slice(procList, func(i, j int) bool { + return procList[i].Name < procList[j].Name + }) +} diff --git a/utility/sort/sort_test.go b/utility/sort/sort_test.go new file mode 100644 index 00000000..481fc597 --- /dev/null +++ b/utility/sort/sort_test.go @@ -0,0 +1,19 @@ +package sort + +import ( + "github.com/gojektech/proctor/proctord/jobs/metadata" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestSorting(t *testing.T) { + procOne := metadata.Metadata{Name: "one"} + procTwo := metadata.Metadata{Name: "two"} + procThree := metadata.Metadata{Name: "three"} + procList := []metadata.Metadata{procThree, procTwo, procOne} + expectedProcList := []metadata.Metadata{procOne, procThree, procTwo} + + Procs(procList) + + assert.Equal(t, expectedProcList, procList) +}