2018-04-04 19:25:51 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes 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 implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-05-02 19:15:47 +00:00
|
|
|
package genericclioptions
|
2018-04-04 19:25:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-04-04 19:25:51 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-07-03 21:50:30 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2018-05-08 20:05:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2018-05-02 19:15:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
2018-04-04 19:25:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NoCompatiblePrinterError struct {
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
OutputFormat *string
|
|
|
|
AllowedFormats []string
|
|
|
|
Options interface{}
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e NoCompatiblePrinterError) Error() string {
|
2018-04-24 19:38:38 +00:00
|
|
|
output := ""
|
|
|
|
if e.OutputFormat != nil {
|
|
|
|
output = *e.OutputFormat
|
|
|
|
}
|
|
|
|
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
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, ","))
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsNoCompatiblePrinterError(err error) bool {
|
2018-04-05 22:39:17 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:25:51 +00:00
|
|
|
_, ok := err.(NoCompatiblePrinterError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintFlags composes common printer flag structs
|
|
|
|
// used across all commands, and provides a method
|
|
|
|
// of retrieving a known printer based on flag values provided.
|
|
|
|
type PrintFlags struct {
|
2018-07-02 14:05:24 +00:00
|
|
|
JSONYamlPrintFlags *JSONYamlPrintFlags
|
|
|
|
NamePrintFlags *NamePrintFlags
|
|
|
|
TemplatePrinterFlags *KubeTemplatePrintFlags
|
2018-04-04 19:25:51 +00:00
|
|
|
|
2018-05-02 19:15:47 +00:00
|
|
|
TypeSetterPrinter *printers.TypeSetterPrinter
|
2018-05-16 18:19:21 +00:00
|
|
|
|
2018-07-03 21:50:30 +00:00
|
|
|
OutputFormat *string
|
|
|
|
outputFlag *pflag.Flag
|
|
|
|
outputDefaulted bool
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 22:39:17 +00:00
|
|
|
func (f *PrintFlags) Complete(successTemplate string) error {
|
|
|
|
return f.NamePrintFlags.Complete(successTemplate)
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
func (f *PrintFlags) AllowedFormats() []string {
|
2018-07-02 14:05:24 +00:00
|
|
|
ret := []string{}
|
|
|
|
ret = append(ret, f.JSONYamlPrintFlags.AllowedFormats()...)
|
|
|
|
ret = append(ret, f.NamePrintFlags.AllowedFormats()...)
|
|
|
|
ret = append(ret, f.TemplatePrinterFlags.AllowedFormats()...)
|
|
|
|
return ret
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:15:47 +00:00
|
|
|
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
2018-04-04 19:25:51 +00:00
|
|
|
outputFormat := ""
|
|
|
|
if f.OutputFormat != nil {
|
|
|
|
outputFormat = *f.OutputFormat
|
|
|
|
}
|
2018-07-03 21:50:30 +00:00
|
|
|
// For backwards compatibility we want to support a --template argument given, even when no --output format is provided.
|
|
|
|
// If a default output format has been set, but no explicit output format has been provided via the --output flag, fallback
|
|
|
|
// to honoring the --template argument.
|
|
|
|
if f.TemplatePrinterFlags != nil && f.TemplatePrinterFlags.TemplateArgument != nil &&
|
|
|
|
len(*f.TemplatePrinterFlags.TemplateArgument) > 0 &&
|
|
|
|
(len(outputFormat) == 0 || (f.outputDefaulted && !f.outputFlag.Changed)) {
|
2018-07-02 14:05:24 +00:00
|
|
|
outputFormat = "go-template"
|
|
|
|
}
|
2018-04-04 19:25:51 +00:00
|
|
|
|
2018-05-01 19:48:46 +00:00
|
|
|
if f.JSONYamlPrintFlags != nil {
|
|
|
|
if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
2018-05-16 18:19:21 +00:00
|
|
|
return f.TypeSetterPrinter.WrapToPrinter(p, err)
|
2018-05-01 19:48:46 +00:00
|
|
|
}
|
2018-04-05 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 19:48:46 +00:00
|
|
|
if f.NamePrintFlags != nil {
|
|
|
|
if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
2018-05-16 18:19:21 +00:00
|
|
|
return f.TypeSetterPrinter.WrapToPrinter(p, err)
|
2018-05-01 19:48:46 +00:00
|
|
|
}
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:05:24 +00:00
|
|
|
if f.TemplatePrinterFlags != nil {
|
|
|
|
if p, err := f.TemplatePrinterFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
|
|
|
|
return f.TypeSetterPrinter.WrapToPrinter(p, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
```
2018-05-27 10:54:44 +00:00
|
|
|
return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()}
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
|
|
|
|
f.JSONYamlPrintFlags.AddFlags(cmd)
|
|
|
|
f.NamePrintFlags.AddFlags(cmd)
|
2018-07-02 14:05:24 +00:00
|
|
|
f.TemplatePrinterFlags.AddFlags(cmd)
|
2018-04-04 19:25:51 +00:00
|
|
|
|
|
|
|
if f.OutputFormat != nil {
|
2018-07-02 11:35:31 +00:00
|
|
|
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|")))
|
2018-07-03 21:50:30 +00:00
|
|
|
f.outputFlag = cmd.Flag("output")
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 00:02:37 +00:00
|
|
|
// WithDefaultOutput sets a default output format if one is not provided through a flag value
|
|
|
|
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
|
2018-05-02 18:49:49 +00:00
|
|
|
f.OutputFormat = &output
|
2018-07-03 21:50:30 +00:00
|
|
|
f.outputDefaulted = true
|
2018-04-19 00:02:37 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:19:21 +00:00
|
|
|
// WithTypeSetter sets a wrapper than will surround the returned printer with a printer to type resources
|
|
|
|
func (f *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags {
|
2018-05-02 19:15:47 +00:00
|
|
|
f.TypeSetterPrinter = printers.NewTypeSetter(scheme)
|
2018-05-16 18:19:21 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPrintFlags(operation string) *PrintFlags {
|
2018-04-04 19:25:51 +00:00
|
|
|
outputFormat := ""
|
|
|
|
|
|
|
|
return &PrintFlags{
|
|
|
|
OutputFormat: &outputFormat,
|
|
|
|
|
2018-07-02 14:05:24 +00:00
|
|
|
JSONYamlPrintFlags: NewJSONYamlPrintFlags(),
|
|
|
|
NamePrintFlags: NewNamePrintFlags(operation),
|
|
|
|
TemplatePrinterFlags: NewKubeTemplatePrintFlags(),
|
2018-04-04 19:25:51 +00:00
|
|
|
}
|
|
|
|
}
|