fix kubectl -o

Fix kubectl -o error message:
Before this change:
```shell
kubectl get pods -o foo
error: unable to match a printer suitable for the output format "" and the options specified: &get.PrintFlags{JSONYamlPrintFlags:(*genericclioptions.JSONYamlPrintFlags)(0x23aa610), NamePrintFlags:(*genericclioptions.NamePrintFlags)(0xc42058b4e0), TemplateFlags:(*printers.KubeTemplatePrintFlags)(0xc4206765e0), CustomColumnsFlags:(*printers.CustomColumnsPrintFlags)(0xc420676620), HumanReadableFlags:(*get.HumanPrintFlags)(0xc4204eb180), NoHeaders:(*bool)(0xc4206fefbc), OutputFormat:(*string)(0xc42058b4d0)}
```

After this change:
```shell
Kubectl get pods -o foo
error: unable to match a printer suitable for the output format "aaa", allowed formats are: custom-columns,custom-columns-file,go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,wide,yaml
```
pull/8/head
Cao Shufeng 2018-05-27 18:54:44 +08:00
parent 84ec43c75b
commit 995c5a07ea
11 changed files with 111 additions and 42 deletions

View File

@ -42,6 +42,13 @@ func (f *kubectlConfigPrintFlags) Complete(successTemplate string) error {
return f.NamePrintFlags.Complete(successTemplate)
}
func (f *kubectlConfigPrintFlags) AllowedFormats() []string {
formats := f.JSONYamlPrintFlags.AllowedFormats()
formats = append(formats, f.NamePrintFlags.AllowedFormats()...)
formats = append(formats, f.TemplateFlags.AllowedFormats()...)
return formats
}
func (f *kubectlConfigPrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
outputFormat := ""
if f.OutputFormat != nil {
@ -60,7 +67,7 @@ func (f *kubectlConfigPrintFlags) ToPrinter() (printers.ResourcePrinter, error)
return f.TypeSetter.WrapToPrinter(p, err)
}
return nil, genericclioptions.NoCompatiblePrinterError{Options: f}
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
}
func (f *kubectlConfigPrintFlags) AddFlags(cmd *cobra.Command) {

View File

@ -38,6 +38,11 @@ type PrintFlags struct {
OutputFormat *string
}
func (f *PrintFlags) AllowedFormats() []string {
return append(append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...),
f.TemplateFlags.AllowedFormats()...)
}
func (f *PrintFlags) Complete(successTemplate string) error {
return f.NamePrintFlags.Complete(successTemplate)
}
@ -60,7 +65,7 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
return f.TypeSetter.WrapToPrinter(p, err)
}
return nil, genericclioptions.NoCompatiblePrinterError{Options: f}
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
}
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {

View File

@ -64,6 +64,15 @@ func (f *PrintFlags) Copy() PrintFlags {
return printFlags
}
func (f *PrintFlags) AllowedFormats() []string {
formats := f.JSONYamlPrintFlags.AllowedFormats()
formats = append(formats, f.NamePrintFlags.AllowedFormats()...)
formats = append(formats, f.TemplateFlags.AllowedFormats()...)
formats = append(formats, f.CustomColumnsFlags.AllowedFormats()...)
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
return formats
}
// UseOpenAPIColumns modifies the output format, as well as the
// "allowMissingKeys" option for template printers, to values
// defined in the OpenAPI schema of a resource.
@ -132,7 +141,7 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
return p, err
}
return nil, genericclioptions.NoCompatiblePrinterError{Options: f}
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
}
// AddFlags receives a *cobra.Command reference and binds

View File

@ -61,11 +61,15 @@ func (f *HumanPrintFlags) EnsureWithNamespace() error {
return nil
}
func (f *HumanPrintFlags) AllowedFormats() []string {
return []string{"wide"}
}
// ToPrinter receives an outputFormat and returns a printer capable of
// handling human-readable output.
func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
if len(outputFormat) > 0 && outputFormat != "wide" {
return nil, genericclioptions.NoCompatiblePrinterError{Options: f}
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, AllowedFormats: f.AllowedFormats()}
}
decoder := scheme.Codecs.UniversalDecoder()

View File

@ -24,6 +24,10 @@ import (
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
)
func (f *JSONYamlPrintFlags) AllowedFormats() []string {
return []string{"json", "yaml"}
}
// JSONYamlPrintFlags provides default flags necessary for json/yaml printing.
// Given the following flag values, a printer can be requested that knows
// how to handle printing based on these values.
@ -44,7 +48,7 @@ func (f *JSONYamlPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePr
case "yaml":
printer = &printers.YAMLPrinter{}
default:
return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &outputFormat}
return nil, NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
}
return printer, nil

View File

@ -40,6 +40,10 @@ func (f *NamePrintFlags) Complete(successTemplate string) error {
return nil
}
func (f *NamePrintFlags) AllowedFormats() []string {
return []string{"name"}
}
// ToPrinter receives an outputFormat and returns a printer capable of
// handling --output=name printing.
// Returns false if the specified outputFormat does not match a supported format.
@ -57,7 +61,7 @@ func (f *NamePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinte
case "":
return namePrinter, nil
default:
return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &outputFormat}
return nil, NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
}
}

View File

@ -18,6 +18,8 @@ package genericclioptions
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
@ -25,8 +27,9 @@ import (
)
type NoCompatiblePrinterError struct {
OutputFormat *string
Options interface{}
OutputFormat *string
AllowedFormats []string
Options interface{}
}
func (e NoCompatiblePrinterError) Error() string {
@ -35,7 +38,8 @@ func (e NoCompatiblePrinterError) Error() string {
output = *e.OutputFormat
}
return fmt.Sprintf("unable to match a printer suitable for the output format %q and the options specified: %#v", output, e.Options)
sort.Strings(e.AllowedFormats)
return fmt.Sprintf("unable to match a printer suitable for the output format %q, allowed formats are: %s", output, strings.Join(e.AllowedFormats, ","))
}
func IsNoCompatiblePrinterError(err error) bool {
@ -63,6 +67,10 @@ func (f *PrintFlags) Complete(successTemplate string) error {
return f.NamePrintFlags.Complete(successTemplate)
}
func (f *PrintFlags) AllowedFormats() []string {
return append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...)
}
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
outputFormat := ""
if f.OutputFormat != nil {
@ -81,7 +89,7 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
}
}
return nil, NoCompatiblePrinterError{Options: f, OutputFormat: f.OutputFormat}
return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()}
}
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {

View File

@ -27,6 +27,11 @@ import (
"k8s.io/kubernetes/pkg/kubectl/scheme"
)
var columnsFormats = map[string]bool{
"custom-columns-file": true,
"custom-columns": true,
}
// CustomColumnsPrintFlags provides default flags necessary for printing
// custom resource columns from an inline-template or file.
type CustomColumnsPrintFlags struct {
@ -34,6 +39,14 @@ type CustomColumnsPrintFlags struct {
TemplateArgument string
}
func (f *CustomColumnsPrintFlags) AllowedFormats() []string {
formats := make([]string, 0, len(columnsFormats))
for format := range columnsFormats {
formats = append(formats, format)
}
return formats
}
// ToPrinter receives an templateFormat and returns a printer capable of
// handling custom-column printing.
// Returns false if the specified templateFormat does not match a supported format.
@ -45,13 +58,8 @@ func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (ResourcePrin
templateValue := ""
supportedFormats := map[string]bool{
"custom-columns-file": true,
"custom-columns": true,
}
if len(f.TemplateArgument) == 0 {
for format := range supportedFormats {
for format := range columnsFormats {
format = format + "="
if strings.HasPrefix(templateFormat, format) {
templateValue = templateFormat[len(format):]
@ -63,8 +71,8 @@ func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (ResourcePrin
templateValue = f.TemplateArgument
}
if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{}
if _, supportedFormat := columnsFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
}
if len(templateValue) == 0 {

View File

@ -26,6 +26,14 @@ import (
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
// templates are logically optional for specifying a format.
// this allows a user to specify a template format value
// as --output=jsonpath=
var jsonFormats = map[string]bool{
"jsonpath": true,
"jsonpath-file": true,
}
// JSONPathPrintFlags provides default flags necessary for template printing.
// Given the following flag values, a printer can be requested that knows
// how to handle printing based on these values.
@ -36,6 +44,14 @@ type JSONPathPrintFlags struct {
TemplateArgument *string
}
func (f *JSONPathPrintFlags) AllowedFormats() []string {
formats := make([]string, 0, len(jsonFormats))
for format := range jsonFormats {
formats = append(formats, format)
}
return formats
}
// ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing.
// Returns false if the specified templateFormat does not match a template format.
@ -46,16 +62,8 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
templateValue := ""
// templates are logically optional for specifying a format.
// this allows a user to specify a template format value
// as --output=jsonpath=
templateFormats := map[string]bool{
"jsonpath": true,
"jsonpath-file": true,
}
if f.TemplateArgument == nil || len(*f.TemplateArgument) == 0 {
for format := range templateFormats {
for format := range jsonFormats {
format = format + "="
if strings.HasPrefix(templateFormat, format) {
templateValue = templateFormat[len(format):]
@ -67,8 +75,8 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
templateValue = *f.TemplateArgument
}
if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
if _, supportedFormat := jsonFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
}
if len(templateValue) == 0 {

View File

@ -33,6 +33,10 @@ type KubeTemplatePrintFlags struct {
TemplateArgument *string
}
func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
}
func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error) {
if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
return p, err

View File

@ -26,6 +26,16 @@ import (
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)
// templates are logically optional for specifying a format.
// this allows a user to specify a template format value
// as --output=go-template=
var templateFormats = map[string]bool{
"template": true,
"go-template": true,
"go-template-file": true,
"templatefile": true,
}
// GoTemplatePrintFlags provides default flags necessary for template printing.
// Given the following flag values, a printer can be requested that knows
// how to handle printing based on these values.
@ -36,6 +46,14 @@ type GoTemplatePrintFlags struct {
TemplateArgument *string
}
func (f *GoTemplatePrintFlags) AllowedFormats() []string {
formats := make([]string, 0, len(templateFormats))
for format := range templateFormats {
formats = append(formats, format)
}
return formats
}
// ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing.
// Returns false if the specified templateFormat does not match a template format.
@ -46,18 +64,8 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
templateValue := ""
// templates are logically optional for specifying a format.
// this allows a user to specify a template format value
// as --output=go-template=
supportedFormats := map[string]bool{
"template": true,
"go-template": true,
"go-template-file": true,
"templatefile": true,
}
if f.TemplateArgument == nil || len(*f.TemplateArgument) == 0 {
for format := range supportedFormats {
for format := range templateFormats {
format = format + "="
if strings.HasPrefix(templateFormat, format) {
templateValue = templateFormat[len(format):]
@ -69,8 +77,8 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
templateValue = *f.TemplateArgument
}
if _, supportedFormat := supportedFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
}
if len(templateValue) == 0 {