mirror of https://github.com/knative/client.git
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
// 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(),
|
|
}
|
|
}
|