Add format functions

Add functions to go templates such as truncating a field.  Also add
the table keyword, which, if placed at the beginning of a format string,
adds headers to the output

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-08-15 16:53:17 -04:00
parent 6ca462a3b6
commit 08c3d241a4
10 changed files with 298 additions and 1046 deletions

View file

@ -3,9 +3,11 @@ package formats
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"os"
"strings"
"text/template"
"github.com/pkg/errors"
)
// Writer interface for outputs
@ -22,6 +24,7 @@ type JSONstruct struct {
type StdoutTemplate struct {
Output []interface{}
Template string
Fields map[string]string
}
// Out method for JSON
@ -36,14 +39,23 @@ func (j JSONstruct) Out() error {
// Out method for Go templates
func (t StdoutTemplate) Out() error {
tmpl, err := template.New("image").Parse(t.Template)
if strings.HasPrefix(t.Template, "table") {
t.Template = strings.TrimSpace(t.Template[5:])
headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template)
if err != nil {
errors.Wrapf(err, "Template parsing error")
}
err = headerTmpl.Execute(os.Stdout, t.Fields)
fmt.Println()
}
tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template)
if err != nil {
return errors.Wrapf(err, "Template parsing error")
}
for _, img := range t.Output {
err = tmpl.Execute(os.Stdout, img)
basicTmpl := tmpl.Funcs(basicFunctions)
err = basicTmpl.Execute(os.Stdout, img)
if err != nil {
return err
}