Skip to content

Commit

Permalink
list services sorted by alphabetical order (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ying Chun Guo authored and knative-prow-robot committed Aug 9, 2019
1 parent 3594b39 commit 8c362f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/kn/commands/service/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service

import (
"fmt"
"sort"

"github.com/knative/client/pkg/kn/commands"
v1alpha12 "github.com/knative/client/pkg/serving/v1alpha1"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions pkg/kn/commands/service/service_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 8c362f0

Please sign in to comment.