Display an external version of the object for --output
Use the version of the API the RESTMapper prefers (currently)
This commit is contained in:
@@ -34,20 +34,10 @@ import (
|
||||
"gopkg.in/v1/yaml"
|
||||
)
|
||||
|
||||
// Print outputs a runtime.Object to an io.Writer in the given format
|
||||
func Print(w io.Writer, obj runtime.Object, format string, templateFile string, defaultPrinter ResourcePrinter) error {
|
||||
printer, err := getPrinter(format, templateFile, defaultPrinter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := printer.PrintObj(obj, w); err != nil {
|
||||
return fmt.Errorf("Failed to print: %v\nRaw received object:\n%#v", err, obj)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (ResourcePrinter, error) {
|
||||
// GetPrinter returns a resource printer and a bool indicating whether the object must be
|
||||
// versioned for the given format.
|
||||
func GetPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (ResourcePrinter, bool, error) {
|
||||
versioned := true
|
||||
var printer ResourcePrinter
|
||||
switch format {
|
||||
case "json":
|
||||
@@ -57,11 +47,11 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
|
||||
case "template":
|
||||
var data []byte
|
||||
if len(templateFile) == 0 {
|
||||
return printer, fmt.Errorf("template format specified but no template given")
|
||||
return nil, false, fmt.Errorf("template format specified but no template given")
|
||||
}
|
||||
tmpl, err := template.New("output").Parse(templateFile)
|
||||
if err != nil {
|
||||
return printer, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
|
||||
return nil, false, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
|
||||
}
|
||||
printer = &TemplatePrinter{
|
||||
Template: tmpl,
|
||||
@@ -72,27 +62,30 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
|
||||
var err error
|
||||
data, err = ioutil.ReadFile(templateFile)
|
||||
if err != nil {
|
||||
return printer, fmt.Errorf("Error reading template %s, %v\n", templateFile, err)
|
||||
return nil, false, fmt.Errorf("Error reading template %s, %v\n", templateFile, err)
|
||||
}
|
||||
} else {
|
||||
return printer, fmt.Errorf("templatefile format specified but no template file given")
|
||||
return nil, false, fmt.Errorf("templatefile format specified but no template file given")
|
||||
}
|
||||
tmpl, err := template.New("output").Parse(string(data))
|
||||
if err != nil {
|
||||
return printer, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
|
||||
return nil, false, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
|
||||
}
|
||||
printer = &TemplatePrinter{
|
||||
Template: tmpl,
|
||||
}
|
||||
default:
|
||||
case "":
|
||||
printer = defaultPrinter
|
||||
versioned = false
|
||||
default:
|
||||
return nil, false, fmt.Errorf("output format %q not recognized", format)
|
||||
}
|
||||
return printer, nil
|
||||
return printer, versioned, nil
|
||||
}
|
||||
|
||||
// ResourcePrinter is an interface that knows how to print API resources.
|
||||
type ResourcePrinter interface {
|
||||
// Print receives an arbitrary JSON body, formats it and prints it to a writer.
|
||||
// Print receives an arbitrary object, formats it and prints it to a writer.
|
||||
PrintObj(runtime.Object, io.Writer) error
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user