Skip to content

Commit

Permalink
change to flags/listprint.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Apr 7, 2020
1 parent af689d6 commit db58f4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
21 changes: 21 additions & 0 deletions pkg/kn/commands/flags/listprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +60,23 @@ func (f *ListPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
return p, nil
}

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) {
Expand Down
16 changes: 1 addition & 15 deletions pkg/kn/commands/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit db58f4c

Please sign in to comment.