diff --git a/docs/cmd/kn_revision_list.md b/docs/cmd/kn_revision_list.md index 59b0d8afb8..a0f289c4e9 100644 --- a/docs/cmd/kn_revision_list.md +++ b/docs/cmd/kn_revision_list.md @@ -7,7 +7,7 @@ List available revisions. List revisions for a given service. ``` -kn revision list [flags] +kn revision list NAME [flags] ``` ### Examples @@ -19,6 +19,12 @@ kn revision list [flags] # List revisions for a service 'svc1' in namespace 'myapp' kn revision list -s svc1 -n myapp + + # List all revisions in JSON output format + kn revision list -o json + + # List revision 'web' + kn revision list web ``` ### Options diff --git a/docs/cmd/kn_service_list.md b/docs/cmd/kn_service_list.md index e78a351187..025f7b2b18 100644 --- a/docs/cmd/kn_service_list.md +++ b/docs/cmd/kn_service_list.md @@ -7,7 +7,21 @@ List available services. List available services. ``` -kn service list [flags] +kn service list NAME [flags] +``` + +### Examples + +``` + + # List all services + kn service list + + # List all services in JSON output format + kn service list -o json + + # List service 'web' + kn service list web ``` ### Options diff --git a/pkg/kn/commands/revision/revision_list.go b/pkg/kn/commands/revision/revision_list.go index dd2e0c9201..ad6ba303af 100644 --- a/pkg/kn/commands/revision/revision_list.go +++ b/pkg/kn/commands/revision/revision_list.go @@ -29,7 +29,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { revisionListFlags := NewRevisionListFlags() revisionListCommand := &cobra.Command{ - Use: "list", + Use: "list NAME", Short: "List available revisions.", Long: "List revisions for a given service.", Example: ` @@ -37,7 +37,13 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { kn revision list # List revisions for a service 'svc1' in namespace 'myapp' - kn revision list -s svc1 -n myapp`, + kn revision list -s svc1 -n myapp + + # List all revisions in JSON output format + kn revision list -o json + + # List revision 'web' + kn revision list web`, RunE: func(cmd *cobra.Command, args []string) error { namespace, err := p.GetNamespace(cmd) if err != nil { @@ -65,7 +71,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { return nil } } else { - revisionList, err = client.ListRevisions() + revisionList, err = getRevisionInfo(args, client) if err != nil { return err } @@ -74,7 +80,6 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { return nil } } - printer, err := revisionListFlags.ToPrinter() if err != nil { return err @@ -90,3 +95,19 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command { revisionListFlags.AddFlags(revisionListCommand) return revisionListCommand } + +func getRevisionInfo(args []string, client v1alpha12.KnClient) (*v1alpha1.RevisionList, error) { + var ( + revisionList *v1alpha1.RevisionList + err error + ) + switch len(args) { + case 0: + revisionList, err = client.ListRevisions() + case 1: + revisionList, err = client.ListRevisions(v1alpha12.WithName(args[0])) + default: + return nil, fmt.Errorf("'kn revision list' accepts maximum 1 argument") + } + return revisionList, err +} diff --git a/pkg/kn/commands/revision/revision_list_test.go b/pkg/kn/commands/revision/revision_list_test.go index af0b86c2f5..e6a7ac44bd 100644 --- a/pkg/kn/commands/revision/revision_list_test.go +++ b/pkg/kn/commands/revision/revision_list_test.go @@ -61,6 +61,19 @@ func TestRevisionListEmpty(t *testing.T) { } } +func TestRevisionListEmptyByName(t *testing.T) { + action, _, err := fakeRevisionList([]string{"revision", "list", "name"}, &v1alpha1.RevisionList{}) + if err != nil { + t.Error(err) + return + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "revisions") { + t.Errorf("Bad action %v", action) + } +} + func TestRevisionListDefaultOutput(t *testing.T) { revision1 := createMockRevisionWithParams("foo-abcd", "foo") revision2 := createMockRevisionWithParams("bar-wxyz", "bar") @@ -123,6 +136,32 @@ func TestRevisionListForService(t *testing.T) { assert.Assert(t, util.ContainsAll(output[0], "No", "revisions", "svc3"), "no revisions") } +func TestRevisionListOneOutput(t *testing.T) { + revision := createMockRevisionWithParams("foo-abcd", "foo") + RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision}} + action, output, err := fakeRevisionList([]string{"revision", "list", "foo-abcd"}, RevisionList) + if err != nil { + t.Fatal(err) + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "revisions") { + t.Errorf("Bad action %v", action) + } + + assert.Assert(t, util.ContainsAll(output[0], "NAME", "SERVICE", "AGE", "CONDITIONS", "READY", "REASON")) + assert.Assert(t, util.ContainsAll(output[1], "foo", "foo-abcd")) +} + +func TestRevisionListOutputWithTwoRevName(t *testing.T) { + t.Log("verify by passing two revision name to get command") + RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{}} + _, _, err := fakeRevisionList([]string{"revision", "list", "foo-abcd", "bar-abcd"}, RevisionList) + if err != nil { + assert.Equal(t, "'kn revision list' accepts maximum 1 argument", err.Error()) + } +} + func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision { revision := &v1alpha1.Revision{ TypeMeta: metav1.TypeMeta{ diff --git a/pkg/kn/commands/service/service_list.go b/pkg/kn/commands/service/service_list.go index 9930277904..59c7752933 100644 --- a/pkg/kn/commands/service/service_list.go +++ b/pkg/kn/commands/service/service_list.go @@ -18,6 +18,8 @@ import ( "fmt" "github.com/knative/client/pkg/kn/commands" + v1alpha12 "github.com/knative/client/pkg/serving/v1alpha1" + "github.com/knative/serving/pkg/apis/serving/v1alpha1" "github.com/spf13/cobra" ) @@ -26,8 +28,17 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { serviceListFlags := NewServiceListFlags() serviceListCommand := &cobra.Command{ - Use: "list", + Use: "list NAME", Short: "List available services.", + Example: ` + # List all services + kn service list + + # List all services in JSON output format + kn service list -o json + + # List service 'web' + kn service list web`, RunE: func(cmd *cobra.Command, args []string) error { namespace, err := p.GetNamespace(cmd) if err != nil { @@ -37,7 +48,7 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { if err != nil { return err } - serviceList, err := client.ListServices() + serviceList, err := getServiceInfo(args, client) if err != nil { return err } @@ -61,3 +72,19 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { serviceListFlags.AddFlags(serviceListCommand) return serviceListCommand } + +func getServiceInfo(args []string, client v1alpha12.KnClient) (*v1alpha1.ServiceList, error) { + var ( + serviceList *v1alpha1.ServiceList + err error + ) + switch len(args) { + case 0: + serviceList, err = client.ListServices() + case 1: + serviceList, err = client.ListServices(v1alpha12.WithName(args[0])) + default: + return nil, fmt.Errorf("'kn service list' accepts maximum 1 argument") + } + return serviceList, err +} diff --git a/pkg/kn/commands/service/service_list_test.go b/pkg/kn/commands/service/service_list_test.go index ec3feede32..fa0753b457 100644 --- a/pkg/kn/commands/service/service_list_test.go +++ b/pkg/kn/commands/service/service_list_test.go @@ -60,6 +60,19 @@ func TestListEmpty(t *testing.T) { } } +func TestGetEmpty(t *testing.T) { + action, _, err := fakeServiceList([]string{"service", "list", "name"}, &v1alpha1.ServiceList{}) + if err != nil { + t.Error(err) + return + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "services") { + t.Errorf("Bad action %v", action) + } +} + func TestServiceListDefaultOutput(t *testing.T) { service1 := createMockServiceWithParams("foo", "foo.default.example.com", 1) service2 := createMockServiceWithParams("bar", "bar.default.example.com", 2) @@ -78,6 +91,31 @@ func TestServiceListDefaultOutput(t *testing.T) { assert.Check(t, util.ContainsAll(output[2], "bar", "bar.default.example.com", "2")) } +func TestServiceGetOneOutput(t *testing.T) { + service := createMockServiceWithParams("foo", "foo.default.example.com", 1) + serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}} + action, output, err := fakeServiceList([]string{"service", "list", "foo"}, serviceList) + if err != nil { + t.Fatal(err) + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "services") { + t.Errorf("Bad action %v", action) + } + assert.Check(t, util.ContainsAll(output[0], "NAME", "DOMAIN", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON")) + assert.Check(t, util.ContainsAll(output[1], "foo", "foo.default.example.com", "1")) +} + +func TestServiceGetWithTwoSrvName(t *testing.T) { + service := createMockServiceWithParams("foo", "foo.default.example.com", 1) + serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service}} + _, _, err := fakeServiceList([]string{"service", "list", "foo", "bar"}, serviceList) + if err != nil { + assert.Equal(t, "'kn service list' accepts maximum 1 argument", err.Error()) + } +} + func createMockServiceWithParams(name, domain string, generation int64) *v1alpha1.Service { service := &v1alpha1.Service{ TypeMeta: metav1.TypeMeta{