From 66d62229f61108647f9a26a1412b4c1605405814 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Fri, 7 Nov 2014 14:41:48 -0800 Subject: [PATCH] Fix kubecfg -template to be versioned --- cmd/kubecfg/kubecfg.go | 9 +++------ pkg/kubecfg/resource_printer.go | 21 +++++++++++++++++---- pkg/kubecfg/resource_printer_test.go | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/cmd/kubecfg/kubecfg.go b/cmd/kubecfg/kubecfg.go index 936df408aa1..8f97cdf05ad 100644 --- a/cmd/kubecfg/kubecfg.go +++ b/cmd/kubecfg/kubecfg.go @@ -26,7 +26,6 @@ import ( "sort" "strconv" "strings" - "text/template" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" @@ -324,14 +323,12 @@ func getPrinter() kubecfg.ResourcePrinter { } else { data = []byte(*templateStr) } - tmpl, err := template.New("output").Parse(string(data)) + var err error + printer, err = kubecfg.NewTemplatePrinter(data) if err != nil { - glog.Fatalf("Error parsing template %s, %v\n", string(data), err) + glog.Fatalf("Error '%v' parsing template:\n'%s'", err, string(data)) return nil } - printer = &kubecfg.TemplatePrinter{ - Template: tmpl, - } default: printer = humanReadablePrinter() } diff --git a/pkg/kubecfg/resource_printer.go b/pkg/kubecfg/resource_printer.go index 048e0327a9d..64ce0915d72 100644 --- a/pkg/kubecfg/resource_printer.go +++ b/pkg/kubecfg/resource_printer.go @@ -319,19 +319,32 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er // TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template. type TemplatePrinter struct { - Template *template.Template + template *template.Template +} + +func NewTemplatePrinter(tmpl []byte) (*TemplatePrinter, error) { + t, err := template.New("output").Parse(string(tmpl)) + if err != nil { + return nil, err + } + return &TemplatePrinter{t}, nil } // Print parses the data as JSON, and re-formats it with the Go Template. func (t *TemplatePrinter) Print(data []byte, w io.Writer) error { - obj, err := latest.Codec.Decode(data) + obj := map[string]interface{}{} + err := json.Unmarshal(data, &obj) if err != nil { return err } - return t.PrintObj(obj, w) + return t.template.Execute(w, obj) } // PrintObj formats the obj with the Go Template. func (t *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error { - return t.Template.Execute(w, obj) + data, err := latest.Codec.Encode(obj) + if err != nil { + return err + } + return t.Print(data, w) } diff --git a/pkg/kubecfg/resource_printer_test.go b/pkg/kubecfg/resource_printer_test.go index 2e05228af89..ef16bdc0884 100644 --- a/pkg/kubecfg/resource_printer_test.go +++ b/pkg/kubecfg/resource_printer_test.go @@ -161,3 +161,19 @@ func TestUnknownTypePrinting(t *testing.T) { t.Errorf("An error was expected from printing unknown type") } } + +func TestTemplateEmitsVersionedObjects(t *testing.T) { + // kind is always blank in memory and set on the wire + printer, err := NewTemplatePrinter([]byte(`{{.kind}}`)) + if err != nil { + t.Fatalf("tmpl fail: %v", err) + } + buffer := &bytes.Buffer{} + err = printer.PrintObj(&api.Pod{}, buffer) + if err != nil { + t.Fatalf("print fail: %v", err) + } + if e, a := "Pod", string(buffer.Bytes()); e != a { + t.Errorf("Expected %v, got %v", e, a) + } +}