2017-02-19 03:40:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package printers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
)
|
|
|
|
|
2017-05-23 02:47:20 +00:00
|
|
|
// GetStandardPrinter takes a format type, an optional format argument. It will return
|
|
|
|
// a printer or an error. The printer is agnostic to schema versions, so you must
|
|
|
|
// send arguments to PrintObj in the version you wish them to be shown using a
|
|
|
|
// VersionedPrinter (typically when generic is true).
|
2018-02-05 16:21:00 +00:00
|
|
|
func GetStandardPrinter(typer runtime.ObjectTyper, encoder runtime.Encoder, decoders []runtime.Decoder, options PrintOptions) (ResourcePrinter, error) {
|
2017-10-30 19:54:44 +00:00
|
|
|
format, formatArgument, allowMissingTemplateKeys := options.OutputFormatType, options.OutputFormatArgument, options.AllowMissingKeys
|
2017-05-19 21:24:39 +00:00
|
|
|
|
2017-02-19 03:40:38 +00:00
|
|
|
var printer ResourcePrinter
|
|
|
|
switch format {
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-20 22:30:00 +00:00
|
|
|
case "json", "yaml":
|
|
|
|
jsonYamlFlags := NewJSONYamlPrintFlags()
|
2018-04-04 19:25:51 +00:00
|
|
|
p, err := jsonYamlFlags.ToPrinter(format)
|
2018-03-20 22:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-20 22:30:00 +00:00
|
|
|
printer = p
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2017-02-19 03:40:38 +00:00
|
|
|
case "name":
|
2018-04-06 15:36:52 +00:00
|
|
|
nameFlags := NewNamePrintFlags("")
|
2018-04-04 19:25:51 +00:00
|
|
|
namePrinter, err := nameFlags.ToPrinter(format)
|
2018-03-21 20:46:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
printer = namePrinter
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-16 22:39:38 +00:00
|
|
|
case "template", "go-template", "jsonpath", "templatefile", "go-template-file", "jsonpath-file":
|
|
|
|
// TODO: construct and bind this separately (at the command level)
|
|
|
|
kubeTemplateFlags := KubeTemplatePrintFlags{
|
|
|
|
GoTemplatePrintFlags: &GoTemplatePrintFlags{
|
|
|
|
AllowMissingKeys: &allowMissingTemplateKeys,
|
|
|
|
TemplateArgument: &formatArgument,
|
|
|
|
},
|
|
|
|
JSONPathPrintFlags: &JSONPathPrintFlags{
|
|
|
|
AllowMissingKeys: &allowMissingTemplateKeys,
|
|
|
|
TemplateArgument: &formatArgument,
|
|
|
|
},
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-04-05 22:39:17 +00:00
|
|
|
kubeTemplatePrinter, err := kubeTemplateFlags.ToPrinter(format)
|
2017-02-19 03:40:38 +00:00
|
|
|
if err != nil {
|
2018-03-16 22:39:38 +00:00
|
|
|
return nil, err
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-16 22:39:38 +00:00
|
|
|
printer = kubeTemplatePrinter
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-21 21:57:58 +00:00
|
|
|
case "custom-columns", "custom-columns-file":
|
|
|
|
customColumnsFlags := &CustomColumnsPrintFlags{
|
|
|
|
NoHeaders: options.NoHeaders,
|
|
|
|
TemplateArgument: formatArgument,
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|
2018-04-19 21:57:09 +00:00
|
|
|
customColumnsPrinter, err := customColumnsFlags.ToPrinter(format)
|
2018-03-21 21:57:58 +00:00
|
|
|
if err != nil {
|
2017-05-23 02:47:20 +00:00
|
|
|
return nil, err
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|
2017-05-23 02:47:20 +00:00
|
|
|
|
2018-03-21 21:57:58 +00:00
|
|
|
printer = customColumnsPrinter
|
|
|
|
|
2017-02-19 03:40:38 +00:00
|
|
|
default:
|
2017-05-23 02:47:20 +00:00
|
|
|
return nil, fmt.Errorf("output format %q not recognized", format)
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|
2017-05-23 02:47:20 +00:00
|
|
|
return printer, nil
|
2017-02-19 03:40:38 +00:00
|
|
|
}
|