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

@ -26,7 +26,7 @@ import (
func (f *Factory) NewCmdGet(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "get [(-o|--output=)console|json|yaml|...] <resource> [<id>]",
Use: "get [(-o|--output=)json|yaml|...] <resource> [<id>]",
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
}

View File

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

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