From c5a93465f8231607278ce3216e9d86d9cc07ca36 Mon Sep 17 00:00:00 2001 From: Daisy Guo Date: Fri, 20 Dec 2019 11:30:15 +0800 Subject: [PATCH] Use the same list flags for list_type, service and route --- .../commands/flags/{list.go => listprint.go} | 14 ++-- .../flags/{list_test.go => listprint_test.go} | 8 +- pkg/kn/commands/route/list.go | 3 +- pkg/kn/commands/route/list_flags.go | 71 ----------------- pkg/kn/commands/route/list_flags_test.go | 47 ----------- pkg/kn/commands/service/list.go | 3 +- pkg/kn/commands/service/list_flags.go | 77 ------------------- pkg/kn/commands/service/list_flags_test.go | 45 ----------- pkg/kn/commands/source/apiserver/list.go | 2 +- pkg/kn/commands/source/cronjob/list.go | 2 +- pkg/kn/commands/source/list_types.go | 3 +- pkg/kn/commands/source/list_types_flags.go | 71 ----------------- .../commands/source/list_types_flags_test.go | 38 --------- pkg/kn/commands/trigger/list.go | 2 +- 14 files changed, 20 insertions(+), 366 deletions(-) rename pkg/kn/commands/flags/{list.go => listprint.go} (86%) rename pkg/kn/commands/flags/{list_test.go => listprint_test.go} (88%) delete mode 100644 pkg/kn/commands/route/list_flags.go delete mode 100644 pkg/kn/commands/route/list_flags_test.go delete mode 100644 pkg/kn/commands/service/list_flags.go delete mode 100644 pkg/kn/commands/service/list_flags_test.go delete mode 100644 pkg/kn/commands/source/list_types_flags.go delete mode 100644 pkg/kn/commands/source/list_types_flags_test.go diff --git a/pkg/kn/commands/flags/list.go b/pkg/kn/commands/flags/listprint.go similarity index 86% rename from pkg/kn/commands/flags/list.go rename to pkg/kn/commands/flags/listprint.go index 5e336f32aa..422de69460 100644 --- a/pkg/kn/commands/flags/list.go +++ b/pkg/kn/commands/flags/listprint.go @@ -23,14 +23,14 @@ 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 @@ -38,7 +38,7 @@ func (f *ListFlags) AllowedFormats() []string { // 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() @@ -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, @@ -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() } diff --git a/pkg/kn/commands/flags/list_test.go b/pkg/kn/commands/flags/listprint_test.go similarity index 88% rename from pkg/kn/commands/flags/list_test.go rename to pkg/kn/commands/flags/listprint_test.go index c17598001e..7a9afb0c2b 100644 --- a/pkg/kn/commands/flags/list_test.go +++ b/pkg/kn/commands/flags/listprint_test.go @@ -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) diff --git a/pkg/kn/commands/route/list.go b/pkg/kn/commands/route/list.go index 7465bf93f1..8bcb5cdb95 100644 --- a/pkg/kn/commands/route/list.go +++ b/pkg/kn/commands/route/list.go @@ -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.", diff --git a/pkg/kn/commands/route/list_flags.go b/pkg/kn/commands/route/list_flags.go deleted file mode 100644 index ab8dea945e..0000000000 --- a/pkg/kn/commands/route/list_flags.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package route - -import ( - "github.com/spf13/cobra" - "k8s.io/cli-runtime/pkg/genericclioptions" - "knative.dev/client/pkg/kn/commands" - hprinters "knative.dev/client/pkg/printers" -) - -// RouteListFlags composes common printer flag structs -// used in the 'kn route list' command. -type RouteListFlags struct { - GenericPrintFlags *genericclioptions.PrintFlags - HumanReadableFlags *commands.HumanPrintFlags -} - -// AllowedFormats is the list of formats in which data can be displayed -func (f *RouteListFlags) AllowedFormats() []string { - formats := f.GenericPrintFlags.AllowedFormats() - formats = append(formats, f.HumanReadableFlags.AllowedFormats()...) - return formats -} - -// ToPrinter attempts to find a composed set of RouteListFlags suitable for -// returning a printer based on current flag values. -func (f *RouteListFlags) ToPrinter() (hprinters.ResourcePrinter, error) { - // if there are flags specified for generic printing - if f.GenericPrintFlags.OutputFlagSpecified() { - p, err := f.GenericPrintFlags.ToPrinter() - if err != nil { - return nil, err - } - return p, nil - } - // if no flags specified, use the table printing - p, err := f.HumanReadableFlags.ToPrinter(RouteListHandlers) - if err != nil { - return nil, err - } - return p, nil -} - -// AddFlags receives a *cobra.Command reference and binds -// flags related to humanreadable and template printing. -func (f *RouteListFlags) AddFlags(cmd *cobra.Command) { - f.GenericPrintFlags.AddFlags(cmd) - f.HumanReadableFlags.AddFlags(cmd) -} - -// NewRouteListFlags returns flags associated with humanreadable, -// template, and "name" printing, with default values set. -func NewRouteListFlags() *RouteListFlags { - return &RouteListFlags{ - GenericPrintFlags: genericclioptions.NewPrintFlags(""), - HumanReadableFlags: commands.NewHumanPrintFlags(), - } -} diff --git a/pkg/kn/commands/route/list_flags_test.go b/pkg/kn/commands/route/list_flags_test.go deleted file mode 100644 index d6024a14c4..0000000000 --- a/pkg/kn/commands/route/list_flags_test.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package route - -import ( - "reflect" - "testing" - - "k8s.io/cli-runtime/pkg/genericclioptions" - "knative.dev/client/pkg/kn/commands" -) - -func TestRoutListFlags(t *testing.T) { - testObject := createMockRouteMeta("foo") - knParams := &commands.KnParams{} - cmd, _, buf := commands.CreateTestKnCommand(NewRouteCommand(knParams), knParams) - routeListFlags := NewRouteListFlags() - routeListFlags.AddFlags(cmd) - printer, err := routeListFlags.ToPrinter() - if genericclioptions.IsNoCompatiblePrinterError(err) { - t.Fatalf("Expected to match human readable printer.") - } - if err != nil { - t.Fatalf("Failed to find a proper printer.") - } - err = printer.PrintObj(testObject, buf) - if err != nil { - t.Fatalf("Failed to print the object.") - } - actualFormats := routeListFlags.AllowedFormats() - expectedFormats := []string{"json", "yaml", "name", "go-template", "go-template-file", "template", "templatefile", "jsonpath", "jsonpath-file", "no-headers"} - if !reflect.DeepEqual(actualFormats, expectedFormats) { - t.Fatalf("Expecting allowed formats:\n%s\nFound:\n%s\n", expectedFormats, actualFormats) - } -} diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go index 7f7d92fa80..9ae2db92bf 100644 --- a/pkg/kn/commands/service/list.go +++ b/pkg/kn/commands/service/list.go @@ -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]", diff --git a/pkg/kn/commands/service/list_flags.go b/pkg/kn/commands/service/list_flags.go deleted file mode 100644 index 1bff01a054..0000000000 --- a/pkg/kn/commands/service/list_flags.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package service - -import ( - "github.com/spf13/cobra" - "k8s.io/cli-runtime/pkg/genericclioptions" - "knative.dev/client/pkg/kn/commands" - hprinters "knative.dev/client/pkg/printers" -) - -// ServiceListFlags composes common printer flag structs -// used in the 'kn service list' command. -type ServiceListFlags struct { - GenericPrintFlags *genericclioptions.PrintFlags - HumanReadableFlags *commands.HumanPrintFlags -} - -// AllowedFormats is the list of formats in which data can be displayed -func (f *ServiceListFlags) AllowedFormats() []string { - formats := f.GenericPrintFlags.AllowedFormats() - formats = append(formats, f.HumanReadableFlags.AllowedFormats()...) - return formats -} - -// ToPrinter attempts to find a composed set of ServiceListFlags suitable for -// returning a printer based on current flag values. -func (f *ServiceListFlags) ToPrinter() (hprinters.ResourcePrinter, error) { - // if there are flags specified for generic printing - if f.GenericPrintFlags.OutputFlagSpecified() { - p, err := f.GenericPrintFlags.ToPrinter() - if err != nil { - return nil, err - } - return p, nil - } - - p, err := f.HumanReadableFlags.ToPrinter(ServiceListHandlers) - if err != nil { - return nil, err - } - return p, nil -} - -// AddFlags receives a *cobra.Command reference and binds -// flags related to humanreadable and template printing. -func (f *ServiceListFlags) AddFlags(cmd *cobra.Command) { - f.GenericPrintFlags.AddFlags(cmd) - f.HumanReadableFlags.AddFlags(cmd) -} - -// NewServiceListFlags returns flags associated with humanreadable, -// template, and "name" printing, with default values set. -func NewServiceListFlags() *ServiceListFlags { - return &ServiceListFlags{ - GenericPrintFlags: genericclioptions.NewPrintFlags(""), - HumanReadableFlags: commands.NewHumanPrintFlags(), - } -} - -// EnsureWithNamespace ensures that humanreadable flags return -// a printer capable of printing with a "namespace" column. -func (f *ServiceListFlags) EnsureWithNamespace() { - f.HumanReadableFlags.EnsureWithNamespace() -} diff --git a/pkg/kn/commands/service/list_flags_test.go b/pkg/kn/commands/service/list_flags_test.go deleted file mode 100644 index 1a898bcedd..0000000000 --- a/pkg/kn/commands/service/list_flags_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package service - -import ( - "reflect" - "testing" - - "github.com/spf13/cobra" - "gotest.tools/assert" -) - -func TestServiceListFlags(t *testing.T) { - var cmd *cobra.Command - - t.Run("verify service list flags", func(t *testing.T) { - serviceListFlags := NewServiceListFlags() - - cmd = &cobra.Command{} - serviceListFlags.AddFlags(cmd) - - assert.Assert(t, serviceListFlags != nil) - assert.Assert(t, cmd.Flags() != nil) - - allowMissingTemplateKeys, err := cmd.Flags().GetBool("allow-missing-template-keys") - assert.NilError(t, err) - assert.Assert(t, allowMissingTemplateKeys == true) - - actualFormats := serviceListFlags.AllowedFormats() - expectedFormats := []string{"json", "yaml", "name", "go-template", "go-template-file", "template", "templatefile", "jsonpath", "jsonpath-file", "no-headers"} - assert.Assert(t, reflect.DeepEqual(actualFormats, expectedFormats)) - }) -} diff --git a/pkg/kn/commands/source/apiserver/list.go b/pkg/kn/commands/source/apiserver/list.go index 5bdc81c5a1..dc2ab77b05 100644 --- a/pkg/kn/commands/source/apiserver/list.go +++ b/pkg/kn/commands/source/apiserver/list.go @@ -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", diff --git a/pkg/kn/commands/source/cronjob/list.go b/pkg/kn/commands/source/cronjob/list.go index d6e0f06381..978b199072 100644 --- a/pkg/kn/commands/source/cronjob/list.go +++ b/pkg/kn/commands/source/cronjob/list.go @@ -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", diff --git a/pkg/kn/commands/source/list_types.go b/pkg/kn/commands/source/list_types.go index 5a00d7bb4f..067a703217 100644 --- a/pkg/kn/commands/source/list_types.go +++ b/pkg/kn/commands/source/list_types.go @@ -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", diff --git a/pkg/kn/commands/source/list_types_flags.go b/pkg/kn/commands/source/list_types_flags.go deleted file mode 100644 index 664897ab8c..0000000000 --- a/pkg/kn/commands/source/list_types_flags.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package source - -import ( - "github.com/spf13/cobra" - "k8s.io/cli-runtime/pkg/genericclioptions" - "knative.dev/client/pkg/kn/commands" - hprinters "knative.dev/client/pkg/printers" -) - -// ListTypesFlags composes common printer flag structs -// used in the 'kn source list-types' command. -type ListTypesFlags struct { - GenericPrintFlags *genericclioptions.PrintFlags - HumanReadableFlags *commands.HumanPrintFlags -} - -// AllowedFormats is the list of formats in which data can be displayed -func (f *ListTypesFlags) 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 *ListTypesFlags) ToPrinter() (hprinters.ResourcePrinter, error) { - // if there are flags specified for generic printing - if f.GenericPrintFlags.OutputFlagSpecified() { - p, err := f.GenericPrintFlags.ToPrinter() - if err != nil { - return nil, err - } - return p, nil - } - - p, err := f.HumanReadableFlags.ToPrinter(ListTypesHandlers) - if err != nil { - return nil, err - } - return p, nil -} - -// AddFlags receives a *cobra.Command reference and binds -// flags related to humanreadable and template printing. -func (f *ListTypesFlags) AddFlags(cmd *cobra.Command) { - f.GenericPrintFlags.AddFlags(cmd) - f.HumanReadableFlags.AddFlags(cmd) -} - -// NewListTypesFlags returns flags associated with humanreadable, -// template, and "name" printing, with default values set. -func NewListTypesFlags() *ListTypesFlags { - return &ListTypesFlags{ - GenericPrintFlags: genericclioptions.NewPrintFlags(""), - HumanReadableFlags: commands.NewHumanPrintFlags(), - } -} diff --git a/pkg/kn/commands/source/list_types_flags_test.go b/pkg/kn/commands/source/list_types_flags_test.go deleted file mode 100644 index f9dc5da5a4..0000000000 --- a/pkg/kn/commands/source/list_types_flags_test.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright © 2019 The Knative Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im -// See the License for the specific language governing permissions and -// limitations under the License. - -package source - -import ( - "testing" - - hprinters "knative.dev/client/pkg/printers" - - "gotest.tools/assert" -) - -func TestListTypesFlagsFormats(t *testing.T) { - flags := NewListTypesFlags() - 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 TestListTypesFlagsPrinter(t *testing.T) { - flags := NewListTypesFlags() - p, err := flags.GenericPrintFlags.ToPrinter() - assert.NilError(t, err) - _, ok := p.(hprinters.ResourcePrinter) - assert.Check(t, ok == true) -} diff --git a/pkg/kn/commands/trigger/list.go b/pkg/kn/commands/trigger/list.go index 089642a190..fa2543657e 100644 --- a/pkg/kn/commands/trigger/list.go +++ b/pkg/kn/commands/trigger/list.go @@ -25,7 +25,7 @@ import ( // NewTriggerListCommand represents 'kn trigger list' command func NewTriggerListCommand(p *commands.KnParams) *cobra.Command { - triggerListFlags := flags.NewListFlags(TriggerListHandlers) + triggerListFlags := flags.NewListPrintFlags(TriggerListHandlers) triggerListCommand := &cobra.Command{ Use: "list [name]",