From 83cfe180b49865ce79cd16de3fc5e493d357b6d4 Mon Sep 17 00:00:00 2001 From: Komal Dhull Date: Fri, 12 Jul 2019 10:33:31 -0400 Subject: [PATCH] Added --no-headers flag for resource listing --- docs/cmd/kn_revision_list.md | 1 + docs/cmd/kn_route_list.md | 1 + docs/cmd/kn_service_list.md | 1 + pkg/kn/commands/human_readable_flags.go | 6 ++++-- pkg/kn/commands/revision/revision_list_test.go | 17 +++++++++++++++++ pkg/kn/commands/route/list_test.go | 17 +++++++++++++++++ pkg/kn/commands/service/service_list_test.go | 17 +++++++++++++++++ pkg/printers/interface.go | 1 + pkg/printers/tableprinter.go | 11 ++++++----- 9 files changed, 65 insertions(+), 7 deletions(-) diff --git a/docs/cmd/kn_revision_list.md b/docs/cmd/kn_revision_list.md index 903931dfda..fed4e3ae20 100644 --- a/docs/cmd/kn_revision_list.md +++ b/docs/cmd/kn_revision_list.md @@ -34,6 +34,7 @@ kn revision list [name] [flags] --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. + --no-headers Remove column headers in default table output. -o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file. -s, --service string Service name --template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. diff --git a/docs/cmd/kn_route_list.md b/docs/cmd/kn_route_list.md index 07babbbfb1..6627ea1049 100644 --- a/docs/cmd/kn_route_list.md +++ b/docs/cmd/kn_route_list.md @@ -31,6 +31,7 @@ kn route list NAME [flags] --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. + --no-headers Remove column headers in default table output. -o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file. --template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. ``` diff --git a/docs/cmd/kn_service_list.md b/docs/cmd/kn_service_list.md index 3bbc2ffd9c..af1821d257 100644 --- a/docs/cmd/kn_service_list.md +++ b/docs/cmd/kn_service_list.md @@ -31,6 +31,7 @@ kn service list [name] [flags] --allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true) -h, --help help for list -n, --namespace string List the requested object(s) in given namespace. + --no-headers Remove column headers in default table output. -o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file. --template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. ``` diff --git a/pkg/kn/commands/human_readable_flags.go b/pkg/kn/commands/human_readable_flags.go index 516bc202d0..4dc912f5f6 100644 --- a/pkg/kn/commands/human_readable_flags.go +++ b/pkg/kn/commands/human_readable_flags.go @@ -30,19 +30,20 @@ import ( // Given the following flag values, a printer can be requested that knows // how to handle printing based on these values. type HumanPrintFlags struct { + NoHeaders bool //TODO: Add more flags as required } // AllowedFormats returns more customized formating options func (f *HumanPrintFlags) AllowedFormats() []string { // TODO: Add more formats eg: wide - return []string{""} + return []string{"no-headers"} } // ToPrinter receives returns a printer capable of // handling human-readable output. func (f *HumanPrintFlags) ToPrinter(getHandlerFunc func(h hprinters.PrintHandler)) (hprinters.ResourcePrinter, error) { - p := hprinters.NewTablePrinter(hprinters.PrintOptions{}) + p := hprinters.NewTablePrinter(hprinters.PrintOptions{NoHeaders: f.NoHeaders}) getHandlerFunc(p) return p, nil } @@ -50,6 +51,7 @@ func (f *HumanPrintFlags) ToPrinter(getHandlerFunc func(h hprinters.PrintHandler // AddFlags receives a *cobra.Command reference and binds // flags related to human-readable printing to it func (f *HumanPrintFlags) AddFlags(c *cobra.Command) { + c.Flags().BoolVar(&f.NoHeaders, "no-headers", false, "Remove column headers in default table output.") //TODO: Add more flags as required } diff --git a/pkg/kn/commands/revision/revision_list_test.go b/pkg/kn/commands/revision/revision_list_test.go index 5425dfbfd1..0bd656a58c 100644 --- a/pkg/kn/commands/revision/revision_list_test.go +++ b/pkg/kn/commands/revision/revision_list_test.go @@ -92,6 +92,23 @@ func TestRevisionListDefaultOutput(t *testing.T) { assert.Check(t, util.ContainsAll(output[2], "bar-wxyz", "bar")) } +func TestRevisionListDefaultOutputNoHeaders(t *testing.T) { + revision1 := createMockRevisionWithParams("foo-abcd", "foo") + revision2 := createMockRevisionWithParams("bar-wxyz", "bar") + RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2}} + action, output, err := fakeRevisionList([]string{"revision", "list", "--no-headers"}, 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.Check(t, util.ContainsAll(output[0], "foo-abcd", "foo")) + assert.Check(t, util.ContainsAll(output[1], "bar-wxyz", "bar")) +} + func TestRevisionListForService(t *testing.T) { revision1 := createMockRevisionWithParams("foo-abcd", "svc1") revision2 := createMockRevisionWithParams("bar-wxyz", "svc1") diff --git a/pkg/kn/commands/route/list_test.go b/pkg/kn/commands/route/list_test.go index 1a30aa8095..0390d3144f 100644 --- a/pkg/kn/commands/route/list_test.go +++ b/pkg/kn/commands/route/list_test.go @@ -77,6 +77,23 @@ func TestRouteListDefaultOutput(t *testing.T) { assert.Check(t, util.ContainsAll(output[2], "bar", "100% -> bar-98765")) } +func TestRouteListDefaultOutputNoHeaders(t *testing.T) { + route1 := createMockRouteSingleTarget("foo", "foo-01234", 100) + route2 := createMockRouteSingleTarget("bar", "bar-98765", 100) + routeList := &v1alpha1.RouteList{Items: []v1alpha1.Route{*route1, *route2}} + action, output, err := fakeRouteList([]string{"route", "list", "--no-headers"}, routeList) + if err != nil { + t.Fatal(err) + } + if action == nil { + t.Errorf("No action") + } else if !action.Matches("list", "routes") { + t.Errorf("Bad action %v", action) + } + assert.Check(t, util.ContainsAll(output[0], "foo", "100% -> foo-01234")) + assert.Check(t, util.ContainsAll(output[1], "bar", "100% -> bar-98765")) +} + func TestRouteListWithTwoTargetsOutput(t *testing.T) { route := createMockRouteTwoTarget("foo", "foo-01234", "foo-98765", 20, 80) routeList := &v1alpha1.RouteList{Items: []v1alpha1.Route{*route}} diff --git a/pkg/kn/commands/service/service_list_test.go b/pkg/kn/commands/service/service_list_test.go index 990bddd8fe..1d1571765c 100644 --- a/pkg/kn/commands/service/service_list_test.go +++ b/pkg/kn/commands/service/service_list_test.go @@ -93,6 +93,23 @@ func TestServiceListDefaultOutput(t *testing.T) { assert.Check(t, util.ContainsAll(output[2], "bar", "bar.default.example.com", "2")) } +func TestServiceListDefaultOutputNoHeaders(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}} + action, output, err := fakeServiceList([]string{"service", "list", "--no-headers"}, 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], "foo", "foo.default.example.com", "1")) + assert.Check(t, util.ContainsAll(output[1], "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}} diff --git a/pkg/printers/interface.go b/pkg/printers/interface.go index 70aa97b4dd..91dad164a8 100644 --- a/pkg/printers/interface.go +++ b/pkg/printers/interface.go @@ -38,5 +38,6 @@ func (fn ResourcePrinterFunc) PrintObj(obj runtime.Object, w io.Writer) error { // PrintOptions for different table printing options type PrintOptions struct { + NoHeaders bool //TODO: Add options for eg: with-kind, server-printing, wide etc } diff --git a/pkg/printers/tableprinter.go b/pkg/printers/tableprinter.go index ff49ed52b7..0f3f347c68 100644 --- a/pkg/printers/tableprinter.go +++ b/pkg/printers/tableprinter.go @@ -73,12 +73,13 @@ func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, obj runti if !results[1].IsNil() { return results[1].Interface().(error) } - - var headers []string - for _, column := range handler.columnDefinitions { - headers = append(headers, strings.ToUpper(column.Name)) + if !options.NoHeaders { + var headers []string + for _, column := range handler.columnDefinitions { + headers = append(headers, strings.ToUpper(column.Name)) + } + printHeader(headers, output) } - printHeader(headers, output) if results[1].IsNil() { rows := results[0].Interface().([]metav1beta1.TableRow)