diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index e4444cd5b64..bf2e3d1ef13 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -26,7 +26,7 @@ import ( func (f *Factory) NewCmdGet(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "get [(-o|--output=)console|json|yaml|...] []", + Use: "get [(-o|--output=)json|yaml|...] []", Short: "Display one or many resources", Long: `Display one or many resources. @@ -66,11 +66,9 @@ Examples: checkErr(err) }, } - // TODO Add an --output-version lock which can ensure that regardless of the - // server version, the client output stays the same. - cmd.Flags().StringP("output", "o", "console", "Output format: console|json|yaml|template") - cmd.Flags().Bool("no-headers", false, "When output format is console, don't print headers") - cmd.Flags().StringP("template", "t", "", "Path to template file to use when --output=template") + cmd.Flags().StringP("output", "o", "", "Output format: json|yaml|template|templatefile") + cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers") + cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when --output=template or --output=templatefile") cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on") return cmd } diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 0fd1cbfb9c5..5a34da68000 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -55,6 +55,18 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re case "yaml": printer = &YAMLPrinter{} case "template": + var data []byte + if len(templateFile) == 0 { + return printer, 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) + } + printer = &TemplatePrinter{ + Template: tmpl, + } + case "templatefile": var data []byte if len(templateFile) > 0 { var err error @@ -63,7 +75,7 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re return printer, fmt.Errorf("Error reading template %s, %v\n", templateFile, err) } } else { - return printer, fmt.Errorf("template format specified but no template file given") + return printer, fmt.Errorf("templatefile format specified but no template file given") } tmpl, err := template.New("output").Parse(string(data)) if err != nil { diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index 4110233f982..7fc8dfc6c8a 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -52,6 +52,59 @@ func TestJSONPrinter(t *testing.T) { testPrinter(t, &JSONPrinter{}, json.Unmarshal) } +func TestPrintJSON(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "json", "", nil); err != nil { + t.Errorf("unexpected error: %#v", err) + } + obj := map[string]interface{}{} + if err := json.Unmarshal(buf.Bytes(), &obj); err != nil { + t.Errorf("unexpected error: %#v\n%s", err, buf.String()) + } +} + +func TestPrintYAML(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "yaml", "", nil); err != nil { + t.Errorf("unexpected error: %#v", err) + } + obj := map[string]interface{}{} + if err := yaml.Unmarshal(buf.Bytes(), &obj); err != nil { + t.Errorf("unexpected error: %#v\n%s", err, buf.String()) + } +} + +func TestPrintTemplate(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name }}", nil); err != nil { + t.Errorf("unexpected error: %#v", err) + } + if buf.String() != "foo" { + t.Errorf("unexpected output: %s", buf.String()) + } +} + +func TestPrintEmptyTemplate(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "", nil); err == nil { + t.Errorf("unexpected non-error") + } +} + +func TestPrintBadTemplate(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name", nil); err == nil { + t.Errorf("unexpected non-error") + } +} + +func TestPrintBadTemplateFile(t *testing.T) { + buf := bytes.NewBuffer([]byte{}) + if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "templatefile", "", nil); err == nil { + t.Errorf("unexpected non-error") + } +} + func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) { buf := bytes.NewBuffer([]byte{})