From 8c362f0363ed54ab964371db53d6a9c6b9f0d1b6 Mon Sep 17 00:00:00 2001 From: Ying Chun Guo Date: Fri, 9 Aug 2019 16:00:04 +0800 Subject: [PATCH] list services sorted by alphabetical order (#330) --- CHANGELOG.adoc | 4 ++++ pkg/kn/commands/service/service_list.go | 6 ++++++ pkg/kn/commands/service/service_list_test.go | 13 ++++++++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6974eae28b..ec6da09de8 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -34,6 +34,10 @@ | `kn service describe` | https://github.com/knative/client/pull/252[#252] +| 🐛 +| `kn service list` lists services sorted by alphabetical order +| https://github.com/knative/client/pull/330[#330] + |=== ## v0.2.0 (2019-07-10) diff --git a/pkg/kn/commands/service/service_list.go b/pkg/kn/commands/service/service_list.go index b3498c705a..c371639d49 100644 --- a/pkg/kn/commands/service/service_list.go +++ b/pkg/kn/commands/service/service_list.go @@ -16,6 +16,7 @@ package service import ( "fmt" + "sort" "github.com/knative/client/pkg/kn/commands" v1alpha12 "github.com/knative/client/pkg/serving/v1alpha1" @@ -61,6 +62,11 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { return err } + // Sort serviceList by name + sort.SliceStable(serviceList.Items, func(i, j int) bool { + return serviceList.Items[i].ObjectMeta.Name < serviceList.Items[j].ObjectMeta.Name + }) + err = printer.PrintObj(serviceList, cmd.OutOrStdout()) if err != nil { return err diff --git a/pkg/kn/commands/service/service_list_test.go b/pkg/kn/commands/service/service_list_test.go index 990bddd8fe..47f7714f9b 100644 --- a/pkg/kn/commands/service/service_list_test.go +++ b/pkg/kn/commands/service/service_list_test.go @@ -76,9 +76,10 @@ func TestGetEmpty(t *testing.T) { } func TestServiceListDefaultOutput(t *testing.T) { - service1 := createMockServiceWithParams("foo", "http://foo.default.example.com", 1) - service2 := createMockServiceWithParams("bar", "http://bar.default.example.com", 2) - serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2}} + service1 := createMockServiceWithParams("foo", "http://foo.default.example.com", 2) + service3 := createMockServiceWithParams("sss", "http://sss.default.example.com", 3) + service2 := createMockServiceWithParams("bar", "http://bar.default.example.com", 1) + serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2, *service3}} action, output, err := fakeServiceList([]string{"service", "list"}, serviceList) if err != nil { t.Fatal(err) @@ -88,9 +89,11 @@ func TestServiceListDefaultOutput(t *testing.T) { } else if !action.Matches("list", "services") { t.Errorf("Bad action %v", action) } + // Outputs in alphabetical order assert.Check(t, util.ContainsAll(output[0], "NAME", "URL", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON")) - assert.Check(t, util.ContainsAll(output[1], "foo", "foo.default.example.com", "1")) - assert.Check(t, util.ContainsAll(output[2], "bar", "bar.default.example.com", "2")) + assert.Check(t, util.ContainsAll(output[1], "bar", "bar.default.example.com", "1")) + assert.Check(t, util.ContainsAll(output[2], "foo", "foo.default.example.com", "2")) + assert.Check(t, util.ContainsAll(output[3], "sss", "sss.default.example.com", "3")) } func TestServiceGetOneOutput(t *testing.T) {