diff --git a/pkg/kubectl/cmd/config/flags.go b/pkg/kubectl/cmd/config/flags.go index cf8b00cd6a6..5e2f3ed8067 100644 --- a/pkg/kubectl/cmd/config/flags.go +++ b/pkg/kubectl/cmd/config/flags.go @@ -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) { diff --git a/pkg/kubectl/cmd/create/flags.go b/pkg/kubectl/cmd/create/flags.go index 7c7540c9bdc..e1bf63cf465 100644 --- a/pkg/kubectl/cmd/create/flags.go +++ b/pkg/kubectl/cmd/create/flags.go @@ -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) { diff --git a/pkg/kubectl/cmd/get/get_flags.go b/pkg/kubectl/cmd/get/get_flags.go index e36321c8c13..0c479175b69 100644 --- a/pkg/kubectl/cmd/get/get_flags.go +++ b/pkg/kubectl/cmd/get/get_flags.go @@ -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 diff --git a/pkg/kubectl/cmd/get/humanreadable_flags.go b/pkg/kubectl/cmd/get/humanreadable_flags.go index 43c303a5f92..28b0e8f8b4c 100644 --- a/pkg/kubectl/cmd/get/humanreadable_flags.go +++ b/pkg/kubectl/cmd/get/humanreadable_flags.go @@ -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() diff --git a/pkg/kubectl/genericclioptions/json_yaml_flags.go b/pkg/kubectl/genericclioptions/json_yaml_flags.go index 91a86e43e9c..8205f5191d1 100644 --- a/pkg/kubectl/genericclioptions/json_yaml_flags.go +++ b/pkg/kubectl/genericclioptions/json_yaml_flags.go @@ -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 diff --git a/pkg/kubectl/genericclioptions/name_flags.go b/pkg/kubectl/genericclioptions/name_flags.go index 5b103e73dd7..13c714554f8 100644 --- a/pkg/kubectl/genericclioptions/name_flags.go +++ b/pkg/kubectl/genericclioptions/name_flags.go @@ -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()} } } diff --git a/pkg/kubectl/genericclioptions/print_flags.go b/pkg/kubectl/genericclioptions/print_flags.go index 981f3697f07..453b913e78b 100644 --- a/pkg/kubectl/genericclioptions/print_flags.go +++ b/pkg/kubectl/genericclioptions/print_flags.go @@ -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) { diff --git a/pkg/printers/customcolumn_flags.go b/pkg/printers/customcolumn_flags.go index b982942ae9b..9ef91f97e73 100644 --- a/pkg/printers/customcolumn_flags.go +++ b/pkg/printers/customcolumn_flags.go @@ -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 { diff --git a/pkg/printers/jsonpath_flags.go b/pkg/printers/jsonpath_flags.go index 49a559d6248..d95f737620f 100644 --- a/pkg/printers/jsonpath_flags.go +++ b/pkg/printers/jsonpath_flags.go @@ -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 { diff --git a/pkg/printers/kube_template_flags.go b/pkg/printers/kube_template_flags.go index a5b0c71326c..8b53e60cdc5 100644 --- a/pkg/printers/kube_template_flags.go +++ b/pkg/printers/kube_template_flags.go @@ -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 diff --git a/pkg/printers/template_flags.go b/pkg/printers/template_flags.go index 0e6d825895d..ffd7a24dca6 100644 --- a/pkg/printers/template_flags.go +++ b/pkg/printers/template_flags.go @@ -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 {