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"
This commit is contained in:
Clayton Coleman
2014-10-29 22:32:25 -04:00
parent e46adc4cd0
commit 70aa9cc62c
3 changed files with 70 additions and 7 deletions

View File

@@ -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{})