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

Use the same flag definition for list command of list_type, service and route #582

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import (

// ListFlags composes common printer flag structs
// used in the list command.
type ListFlags struct {
type ListPrintFlags struct {
GenericPrintFlags *genericclioptions.PrintFlags
HumanReadableFlags *commands.HumanPrintFlags
PrinterHandler func(h hprinters.PrintHandler)
}

// AllowedFormats is the list of formats in which data can be displayed
func (f *ListFlags) AllowedFormats() []string {
func (f *ListPrintFlags) AllowedFormats() []string {
formats := f.GenericPrintFlags.AllowedFormats()
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
return formats
}

// ToPrinter attempts to find a composed set of ListTypesFlags suitable for
// returning a printer based on current flag values.
func (f *ListFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
func (f *ListPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
// if there are flags specified for generic printing
if f.GenericPrintFlags.OutputFlagSpecified() {
p, err := f.GenericPrintFlags.ToPrinter()
Expand All @@ -57,15 +57,15 @@ func (f *ListFlags) ToPrinter() (hprinters.ResourcePrinter, error) {

// AddFlags receives a *cobra.Command reference and binds
// flags related to humanreadable and template printing.
func (f *ListFlags) AddFlags(cmd *cobra.Command) {
func (f *ListPrintFlags) AddFlags(cmd *cobra.Command) {
f.GenericPrintFlags.AddFlags(cmd)
f.HumanReadableFlags.AddFlags(cmd)
}

// NewListFlags returns flags associated with humanreadable,
// template, and "name" printing, with default values set.
func NewListFlags(printer func(h hprinters.PrintHandler)) *ListFlags {
return &ListFlags{
func NewListPrintFlags(printer func(h hprinters.PrintHandler)) *ListPrintFlags {
return &ListPrintFlags{
GenericPrintFlags: genericclioptions.NewPrintFlags(""),
HumanReadableFlags: commands.NewHumanPrintFlags(),
PrinterHandler: printer,
Expand All @@ -74,6 +74,6 @@ func NewListFlags(printer func(h hprinters.PrintHandler)) *ListFlags {

// EnsureWithNamespace ensures that humanreadable flags return
// a printer capable of printing with a "namespace" column.
func (f *ListFlags) EnsureWithNamespace() {
func (f *ListPrintFlags) EnsureWithNamespace() {
f.HumanReadableFlags.EnsureWithNamespace()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ import (
hprinters "knative.dev/client/pkg/printers"
)

func TestListFlagsFormats(t *testing.T) {
flags := NewListFlags(nil)
func TestListPrintFlagsFormats(t *testing.T) {
flags := NewListPrintFlags(nil)
formats := flags.AllowedFormats()
expected := []string{"json", "yaml", "name", "go-template", "go-template-file", "template", "templatefile", "jsonpath", "jsonpath-file", "no-headers"}
assert.DeepEqual(t, formats, expected)
}

func TestListFlags(t *testing.T) {
func TestListPrintFlags(t *testing.T) {
var cmd *cobra.Command
flags := NewListFlags(func(h hprinters.PrintHandler) {})
flags := NewListPrintFlags(func(h hprinters.PrintHandler) {})

cmd = &cobra.Command{}
flags.AddFlags(cmd)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/route/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
v1alpha12 "knative.dev/client/pkg/serving/v1alpha1"

"github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands/flags"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
)

// NewrouteListCommand represents 'kn route list' command
func NewRouteListCommand(p *commands.KnParams) *cobra.Command {
routeListFlags := NewRouteListFlags()
routeListFlags := flags.NewListPrintFlags(RouteListHandlers)
routeListCommand := &cobra.Command{
Use: "list NAME",
Short: "List available routes.",
Expand Down
71 changes: 0 additions & 71 deletions pkg/kn/commands/route/list_flags.go

This file was deleted.

47 changes: 0 additions & 47 deletions pkg/kn/commands/route/list_flags_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/kn/commands/service/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (

"github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
v1alpha12 "knative.dev/client/pkg/serving/v1alpha1"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
)

// NewServiceListCommand represents 'kn service list' command
func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
serviceListFlags := NewServiceListFlags()
serviceListFlags := flags.NewListPrintFlags(ServiceListHandlers)

serviceListCommand := &cobra.Command{
Use: "list [name]",
Expand Down
77 changes: 0 additions & 77 deletions pkg/kn/commands/service/list_flags.go

This file was deleted.

45 changes: 0 additions & 45 deletions pkg/kn/commands/service/list_flags_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/kn/commands/source/apiserver/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// NewAPIServerListCommand is for listing ApiServer source COs
func NewAPIServerListCommand(p *commands.KnParams) *cobra.Command {
listFlags := flags.NewListFlags(APIServerSourceListHandlers)
listFlags := flags.NewListPrintFlags(APIServerSourceListHandlers)

listCommand := &cobra.Command{
Use: "list",
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/cronjob/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// NewCronJobListCommand is for listing CronJob source COs
func NewCronJobListCommand(p *commands.KnParams) *cobra.Command {
listFlags := flags.NewListFlags(CronJobSourceListHandlers)
listFlags := flags.NewListPrintFlags(CronJobSourceListHandlers)

listCommand := &cobra.Command{
Use: "list",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/source/list_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (

"github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
)

// NewListTypesCommand defines and processes `kn source list-types` command operations
func NewListTypesCommand(p *commands.KnParams) *cobra.Command {
listTypesFlags := NewListTypesFlags()
listTypesFlags := flags.NewListPrintFlags(ListTypesHandlers)
listTypesCommand := &cobra.Command{
Use: "list-types",
Short: "List available source types",
Expand Down
Loading