Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service/revision list output with '-o' param #1276

Merged
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
| 🎁
| Add context.Context parameter to API function
| https://github.com/knative/client/pull/1274[#1274]

|🐛
| Respect `-o` in `service list` and `revision list` in case if no services/revisions present
| https://github.com/knative/client/pull/1276[#1276]
|===

## v0.21.0 (2021-02-23)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/revision/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
}

// Stop if nothing found
if len(revisionList.Items) == 0 {
if !revisionListFlags.GenericPrintFlags.OutputFlagSpecified() && len(revisionList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No revisions found.\n")
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/kn/commands/revision/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package revision

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -60,6 +61,27 @@ func TestRevisionListEmpty(t *testing.T) {
}
}

func TestRevisionListEmptyWithJSON(t *testing.T) {
action, output, err := fakeRevisionList([]string{"revision", "list", "-o", "json"}, &servingv1.RevisionList{})
assert.NilError(t, err)
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "revisions") {
t.Errorf("Bad action %v", action)
}

var result servingv1.RevisionList
err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result)
assert.NilError(t, err)
assert.DeepEqual(t, result, servingv1.RevisionList{
TypeMeta: metav1.TypeMeta{
APIVersion: "serving.knative.dev/v1",
Kind: "RevisionList",
},
Items: []servingv1.Revision{},
})
}

func TestRevisionListEmptyByName(t *testing.T) {
action, _, err := fakeRevisionList([]string{"revision", "list", "name"}, &servingv1.RevisionList{})
assert.NilError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/kn/commands/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
if len(serviceList.Items) == 0 {

// Stop if nothing found
if !serviceListFlags.GenericPrintFlags.OutputFlagSpecified() && len(serviceList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No services found.\n")
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/kn/commands/service/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package service

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -58,6 +59,27 @@ func TestListEmpty(t *testing.T) {
}
}

func TestListEmptyWithJSON(t *testing.T) {
action, output, err := fakeServiceList([]string{"service", "list", "-o", "json"}, &servingv1.ServiceList{})
assert.NilError(t, err)
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "services") {
t.Errorf("Bad action %v", action)
}

var result servingv1.ServiceList
err = json.Unmarshal([]byte(strings.Join(output[:], "\n")), &result)
assert.NilError(t, err)
assert.DeepEqual(t, result, servingv1.ServiceList{
TypeMeta: metav1.TypeMeta{
APIVersion: "serving.knative.dev/v1",
Kind: "ServiceList",
},
Items: []servingv1.Service{},
})
}

func TestGetEmpty(t *testing.T) {
action, _, err := fakeServiceList([]string{"service", "list", "name"}, &servingv1.ServiceList{})
assert.NilError(t, err)
Expand Down