Skip to content

Commit 70aa9cc

Browse files
Add --template and --templatefile options for more flexibility
Allow directly entered templates for scripting flexibility. Changes --output=template to mean "string" and --output=templatefile to mean "from file"
1 parent e46adc4 commit 70aa9cc

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

pkg/kubectl/cmd/get.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func (f *Factory) NewCmdGet(out io.Writer) *cobra.Command {
2828
cmd := &cobra.Command{
29-
Use: "get [(-o|--output=)console|json|yaml|...] <resource> [<id>]",
29+
Use: "get [(-o|--output=)json|yaml|...] <resource> [<id>]",
3030
Short: "Display one or many resources",
3131
Long: `Display one or many resources.
3232
@@ -66,11 +66,9 @@ Examples:
6666
checkErr(err)
6767
},
6868
}
69-
// TODO Add an --output-version lock which can ensure that regardless of the
70-
// server version, the client output stays the same.
71-
cmd.Flags().StringP("output", "o", "console", "Output format: console|json|yaml|template")
72-
cmd.Flags().Bool("no-headers", false, "When output format is console, don't print headers")
73-
cmd.Flags().StringP("template", "t", "", "Path to template file to use when --output=template")
69+
cmd.Flags().StringP("output", "o", "", "Output format: json|yaml|template|templatefile")
70+
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers")
71+
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when --output=template or --output=templatefile")
7472
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
7573
return cmd
7674
}

pkg/kubectl/resource_printer.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
5555
case "yaml":
5656
printer = &YAMLPrinter{}
5757
case "template":
58+
var data []byte
59+
if len(templateFile) == 0 {
60+
return printer, fmt.Errorf("template format specified but no template given")
61+
}
62+
tmpl, err := template.New("output").Parse(templateFile)
63+
if err != nil {
64+
return printer, fmt.Errorf("Error parsing template %s, %v\n", string(data), err)
65+
}
66+
printer = &TemplatePrinter{
67+
Template: tmpl,
68+
}
69+
case "templatefile":
5870
var data []byte
5971
if len(templateFile) > 0 {
6072
var err error
@@ -63,7 +75,7 @@ func getPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
6375
return printer, fmt.Errorf("Error reading template %s, %v\n", templateFile, err)
6476
}
6577
} else {
66-
return printer, fmt.Errorf("template format specified but no template file given")
78+
return printer, fmt.Errorf("templatefile format specified but no template file given")
6779
}
6880
tmpl, err := template.New("output").Parse(string(data))
6981
if err != nil {

pkg/kubectl/resource_printer_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,59 @@ func TestJSONPrinter(t *testing.T) {
5252
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
5353
}
5454

55+
func TestPrintJSON(t *testing.T) {
56+
buf := bytes.NewBuffer([]byte{})
57+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "json", "", nil); err != nil {
58+
t.Errorf("unexpected error: %#v", err)
59+
}
60+
obj := map[string]interface{}{}
61+
if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
62+
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
63+
}
64+
}
65+
66+
func TestPrintYAML(t *testing.T) {
67+
buf := bytes.NewBuffer([]byte{})
68+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "yaml", "", nil); err != nil {
69+
t.Errorf("unexpected error: %#v", err)
70+
}
71+
obj := map[string]interface{}{}
72+
if err := yaml.Unmarshal(buf.Bytes(), &obj); err != nil {
73+
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
74+
}
75+
}
76+
77+
func TestPrintTemplate(t *testing.T) {
78+
buf := bytes.NewBuffer([]byte{})
79+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name }}", nil); err != nil {
80+
t.Errorf("unexpected error: %#v", err)
81+
}
82+
if buf.String() != "foo" {
83+
t.Errorf("unexpected output: %s", buf.String())
84+
}
85+
}
86+
87+
func TestPrintEmptyTemplate(t *testing.T) {
88+
buf := bytes.NewBuffer([]byte{})
89+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "", nil); err == nil {
90+
t.Errorf("unexpected non-error")
91+
}
92+
}
93+
94+
func TestPrintBadTemplate(t *testing.T) {
95+
buf := bytes.NewBuffer([]byte{})
96+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "template", "{{ .Name", nil); err == nil {
97+
t.Errorf("unexpected non-error")
98+
}
99+
}
100+
101+
func TestPrintBadTemplateFile(t *testing.T) {
102+
buf := bytes.NewBuffer([]byte{})
103+
if err := Print(buf, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, "templatefile", "", nil); err == nil {
104+
t.Errorf("unexpected non-error")
105+
}
106+
}
107+
55108
func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
56109
buf := bytes.NewBuffer([]byte{})
57110

0 commit comments

Comments
 (0)