From e66b3deed158c750f20e578495a6262393c85902 Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Wed, 8 Apr 2020 00:31:08 +0800 Subject: [PATCH] change to flags/listprint.go --- pkg/kn/commands/flags/listprint.go | 22 ++++++++++++++++++++++ pkg/kn/commands/flags/listprint_test.go | 15 +++++++++++++++ pkg/kn/commands/service/list.go | 16 +--------------- pkg/printers/tableprinter.go | 4 ++++ 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/pkg/kn/commands/flags/listprint.go b/pkg/kn/commands/flags/listprint.go index a19cca498c..da73381c01 100644 --- a/pkg/kn/commands/flags/listprint.go +++ b/pkg/kn/commands/flags/listprint.go @@ -15,11 +15,15 @@ package flags import ( + "io" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" "knative.dev/client/pkg/kn/commands" hprinters "knative.dev/client/pkg/printers" + "knative.dev/client/pkg/util" ) // ListFlags composes common printer flag structs @@ -56,6 +60,24 @@ func (f *ListPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) { return p, nil } +// Print is to print an Object to a Writer +func (f *ListPrintFlags) Print(obj runtime.Object, w io.Writer) error { + printer, err := f.ToPrinter() + if err != nil { + return err + } + + if f.GenericPrintFlags.OutputFlagSpecified() { + unstructuredList, err := util.ToUnstructuredList(obj) + if err != nil { + return err + } + return printer.PrintObj(unstructuredList, w) + } + + return printer.PrintObj(obj, w) +} + // AddFlags receives a *cobra.Command reference and binds // flags related to humanreadable and template printing. func (f *ListPrintFlags) AddFlags(cmd *cobra.Command) { diff --git a/pkg/kn/commands/flags/listprint_test.go b/pkg/kn/commands/flags/listprint_test.go index 7a9afb0c2b..da17e88de8 100644 --- a/pkg/kn/commands/flags/listprint_test.go +++ b/pkg/kn/commands/flags/listprint_test.go @@ -48,3 +48,18 @@ func TestListPrintFlags(t *testing.T) { _, ok := p.(hprinters.ResourcePrinter) assert.Check(t, ok == true) } + +func TestListPrintFlagsPrint(t *testing.T) { + var cmd *cobra.Command + flags := NewListPrintFlags(func(h hprinters.PrintHandler) {}) + + cmd = &cobra.Command{} + flags.AddFlags(cmd) + + pr, err := flags.ToPrinter() + assert.NilError(t, err) + assert.Assert(t, pr != nil) + + err = flags.Print(nil, cmd.OutOrStdout()) + assert.NilError(t, err) +} diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go index 974db35340..10a3308cc2 100644 --- a/pkg/kn/commands/service/list.go +++ b/pkg/kn/commands/service/list.go @@ -24,7 +24,6 @@ import ( "knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands/flags" clientservingv1 "knative.dev/client/pkg/serving/v1" - "knative.dev/client/pkg/util" ) // NewServiceListCommand represents 'kn service list' command @@ -66,11 +65,6 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { serviceListFlags.EnsureWithNamespace() } - printer, err := serviceListFlags.ToPrinter() - if err != nil { - return err - } - // Sort serviceList by namespace and name (in this order) sort.SliceStable(serviceList.Items, func(i, j int) bool { a := serviceList.Items[i] @@ -82,15 +76,7 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command { return a.ObjectMeta.Name < b.ObjectMeta.Name }) - if serviceListFlags.GenericPrintFlags.OutputFlagSpecified() { - unstructedList, err := util.ToUnstructuredList(serviceList) - if err != nil { - return err - } - return printer.PrintObj(unstructedList, cmd.OutOrStdout()) - } - - return printer.PrintObj(serviceList, cmd.OutOrStdout()) + return serviceListFlags.Print(serviceList, cmd.OutOrStdout()) }, } commands.AddNamespaceFlags(serviceListCommand.Flags(), true) diff --git a/pkg/printers/tableprinter.go b/pkg/printers/tableprinter.go index f51bc1390f..0f309b40d9 100644 --- a/pkg/printers/tableprinter.go +++ b/pkg/printers/tableprinter.go @@ -41,6 +41,10 @@ func NewTablePrinter(options PrintOptions) *HumanReadablePrinter { // PrintObj prints the obj in a human-friendly format according to the type of the obj. func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error { + if obj == nil { + return nil + } + w, found := output.(*tabwriter.Writer) if !found { w = NewTabWriter(output)