Use the same list flags for list_type, service and route (#582)

This commit is contained in:
Ying Chun Guo 2019-12-20 20:50:33 +08:00 committed by Knative Prow Robot
parent b640219140
commit d03cbf904c
14 changed files with 20 additions and 366 deletions

View File

@ -23,14 +23,14 @@ import (
// ListFlags composes common printer flag structs // ListFlags composes common printer flag structs
// used in the list command. // used in the list command.
type ListFlags struct { type ListPrintFlags struct {
GenericPrintFlags *genericclioptions.PrintFlags GenericPrintFlags *genericclioptions.PrintFlags
HumanReadableFlags *commands.HumanPrintFlags HumanReadableFlags *commands.HumanPrintFlags
PrinterHandler func(h hprinters.PrintHandler) PrinterHandler func(h hprinters.PrintHandler)
} }
// AllowedFormats is the list of formats in which data can be displayed // 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 := f.GenericPrintFlags.AllowedFormats()
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...) formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
return formats return formats
@ -38,7 +38,7 @@ func (f *ListFlags) AllowedFormats() []string {
// ToPrinter attempts to find a composed set of ListTypesFlags suitable for // ToPrinter attempts to find a composed set of ListTypesFlags suitable for
// returning a printer based on current flag values. // 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 there are flags specified for generic printing
if f.GenericPrintFlags.OutputFlagSpecified() { if f.GenericPrintFlags.OutputFlagSpecified() {
p, err := f.GenericPrintFlags.ToPrinter() p, err := f.GenericPrintFlags.ToPrinter()
@ -57,15 +57,15 @@ func (f *ListFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
// AddFlags receives a *cobra.Command reference and binds // AddFlags receives a *cobra.Command reference and binds
// flags related to humanreadable and template printing. // 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.GenericPrintFlags.AddFlags(cmd)
f.HumanReadableFlags.AddFlags(cmd) f.HumanReadableFlags.AddFlags(cmd)
} }
// NewListFlags returns flags associated with humanreadable, // NewListFlags returns flags associated with humanreadable,
// template, and "name" printing, with default values set. // template, and "name" printing, with default values set.
func NewListFlags(printer func(h hprinters.PrintHandler)) *ListFlags { func NewListPrintFlags(printer func(h hprinters.PrintHandler)) *ListPrintFlags {
return &ListFlags{ return &ListPrintFlags{
GenericPrintFlags: genericclioptions.NewPrintFlags(""), GenericPrintFlags: genericclioptions.NewPrintFlags(""),
HumanReadableFlags: commands.NewHumanPrintFlags(), HumanReadableFlags: commands.NewHumanPrintFlags(),
PrinterHandler: printer, PrinterHandler: printer,
@ -74,6 +74,6 @@ func NewListFlags(printer func(h hprinters.PrintHandler)) *ListFlags {
// EnsureWithNamespace ensures that humanreadable flags return // EnsureWithNamespace ensures that humanreadable flags return
// a printer capable of printing with a "namespace" column. // a printer capable of printing with a "namespace" column.
func (f *ListFlags) EnsureWithNamespace() { func (f *ListPrintFlags) EnsureWithNamespace() {
f.HumanReadableFlags.EnsureWithNamespace() f.HumanReadableFlags.EnsureWithNamespace()
} }

View File

@ -22,16 +22,16 @@ import (
hprinters "knative.dev/client/pkg/printers" hprinters "knative.dev/client/pkg/printers"
) )
func TestListFlagsFormats(t *testing.T) { func TestListPrintFlagsFormats(t *testing.T) {
flags := NewListFlags(nil) flags := NewListPrintFlags(nil)
formats := flags.AllowedFormats() formats := flags.AllowedFormats()
expected := []string{"json", "yaml", "name", "go-template", "go-template-file", "template", "templatefile", "jsonpath", "jsonpath-file", "no-headers"} expected := []string{"json", "yaml", "name", "go-template", "go-template-file", "template", "templatefile", "jsonpath", "jsonpath-file", "no-headers"}
assert.DeepEqual(t, formats, expected) assert.DeepEqual(t, formats, expected)
} }
func TestListFlags(t *testing.T) { func TestListPrintFlags(t *testing.T) {
var cmd *cobra.Command var cmd *cobra.Command
flags := NewListFlags(func(h hprinters.PrintHandler) {}) flags := NewListPrintFlags(func(h hprinters.PrintHandler) {})
cmd = &cobra.Command{} cmd = &cobra.Command{}
flags.AddFlags(cmd) flags.AddFlags(cmd)

View File

@ -22,12 +22,13 @@ import (
v1alpha12 "knative.dev/client/pkg/serving/v1alpha1" v1alpha12 "knative.dev/client/pkg/serving/v1alpha1"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands/flags"
"knative.dev/serving/pkg/apis/serving/v1alpha1" "knative.dev/serving/pkg/apis/serving/v1alpha1"
) )
// NewrouteListCommand represents 'kn route list' command // NewrouteListCommand represents 'kn route list' command
func NewRouteListCommand(p *commands.KnParams) *cobra.Command { func NewRouteListCommand(p *commands.KnParams) *cobra.Command {
routeListFlags := NewRouteListFlags() routeListFlags := flags.NewListPrintFlags(RouteListHandlers)
routeListCommand := &cobra.Command{ routeListCommand := &cobra.Command{
Use: "list NAME", Use: "list NAME",
Short: "List available routes.", Short: "List available routes.",

View File

@ -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(),
}
}

View File

@ -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)
}
}

View File

@ -20,13 +20,14 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
v1alpha12 "knative.dev/client/pkg/serving/v1alpha1" v1alpha12 "knative.dev/client/pkg/serving/v1alpha1"
"knative.dev/serving/pkg/apis/serving/v1alpha1" "knative.dev/serving/pkg/apis/serving/v1alpha1"
) )
// NewServiceListCommand represents 'kn service list' command // NewServiceListCommand represents 'kn service list' command
func NewServiceListCommand(p *commands.KnParams) *cobra.Command { func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
serviceListFlags := NewServiceListFlags() serviceListFlags := flags.NewListPrintFlags(ServiceListHandlers)
serviceListCommand := &cobra.Command{ serviceListCommand := &cobra.Command{
Use: "list [name]", Use: "list [name]",

View File

@ -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()
}

View File

@ -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))
})
}

View File

@ -24,7 +24,7 @@ import (
// NewAPIServerListCommand is for listing ApiServer source COs // NewAPIServerListCommand is for listing ApiServer source COs
func NewAPIServerListCommand(p *commands.KnParams) *cobra.Command { func NewAPIServerListCommand(p *commands.KnParams) *cobra.Command {
listFlags := flags.NewListFlags(APIServerSourceListHandlers) listFlags := flags.NewListPrintFlags(APIServerSourceListHandlers)
listCommand := &cobra.Command{ listCommand := &cobra.Command{
Use: "list", Use: "list",

View File

@ -24,7 +24,7 @@ import (
// NewCronJobListCommand is for listing CronJob source COs // NewCronJobListCommand is for listing CronJob source COs
func NewCronJobListCommand(p *commands.KnParams) *cobra.Command { func NewCronJobListCommand(p *commands.KnParams) *cobra.Command {
listFlags := flags.NewListFlags(CronJobSourceListHandlers) listFlags := flags.NewListPrintFlags(CronJobSourceListHandlers)
listCommand := &cobra.Command{ listCommand := &cobra.Command{
Use: "list", Use: "list",

View File

@ -19,11 +19,12 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"knative.dev/client/pkg/kn/commands" "knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
) )
// NewListTypesCommand defines and processes `kn source list-types` command operations // NewListTypesCommand defines and processes `kn source list-types` command operations
func NewListTypesCommand(p *commands.KnParams) *cobra.Command { func NewListTypesCommand(p *commands.KnParams) *cobra.Command {
listTypesFlags := NewListTypesFlags() listTypesFlags := flags.NewListPrintFlags(ListTypesHandlers)
listTypesCommand := &cobra.Command{ listTypesCommand := &cobra.Command{
Use: "list-types", Use: "list-types",
Short: "List available source types", Short: "List available source types",

View File

@ -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(),
}
}

View File

@ -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)
}

View File

@ -25,7 +25,7 @@ import (
// NewTriggerListCommand represents 'kn trigger list' command // NewTriggerListCommand represents 'kn trigger list' command
func NewTriggerListCommand(p *commands.KnParams) *cobra.Command { func NewTriggerListCommand(p *commands.KnParams) *cobra.Command {
triggerListFlags := flags.NewListFlags(TriggerListHandlers) triggerListFlags := flags.NewListPrintFlags(TriggerListHandlers)
triggerListCommand := &cobra.Command{ triggerListCommand := &cobra.Command{
Use: "list [name]", Use: "list [name]",