2017-07-31 18:02:57 +00:00
|
|
|
package formats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-08-15 20:53:17 +00:00
|
|
|
"strings"
|
2017-08-08 13:24:58 +00:00
|
|
|
"text/tabwriter"
|
2017-07-31 18:02:57 +00:00
|
|
|
"text/template"
|
2017-08-15 20:53:17 +00:00
|
|
|
|
2017-08-08 13:24:58 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2017-08-15 20:53:17 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-31 18:02:57 +00:00
|
|
|
)
|
|
|
|
|
2017-08-08 13:24:58 +00:00
|
|
|
const (
|
|
|
|
// JSONString const to save on duplicate variable names
|
|
|
|
JSONString = "json"
|
|
|
|
// IDString const to save on duplicates for Go templates
|
|
|
|
IDString = "{{.ID}}"
|
|
|
|
)
|
2017-08-14 19:32:00 +00:00
|
|
|
|
2017-07-31 18:02:57 +00:00
|
|
|
// Writer interface for outputs
|
|
|
|
type Writer interface {
|
|
|
|
Out() error
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
// JSONStructArray for JSON output
|
|
|
|
type JSONStructArray struct {
|
2017-07-31 18:02:57 +00:00
|
|
|
Output []interface{}
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
// StdoutTemplateArray for Go template output
|
|
|
|
type StdoutTemplateArray struct {
|
|
|
|
Output []interface{}
|
|
|
|
Template string
|
|
|
|
Fields map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONStruct for JSON output
|
|
|
|
type JSONStruct struct {
|
|
|
|
Output interface{}
|
|
|
|
}
|
|
|
|
|
2017-07-31 18:02:57 +00:00
|
|
|
// StdoutTemplate for Go template output
|
|
|
|
type StdoutTemplate struct {
|
2017-08-14 19:32:00 +00:00
|
|
|
Output interface{}
|
2017-07-31 18:02:57 +00:00
|
|
|
Template string
|
2017-08-15 20:53:17 +00:00
|
|
|
Fields map[string]string
|
2017-07-31 18:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
// YAMLStruct for YAML output
|
|
|
|
type YAMLStruct struct {
|
|
|
|
Output interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out method for JSON Arrays
|
|
|
|
func (j JSONStructArray) Out() error {
|
2017-07-31 18:02:57 +00:00
|
|
|
data, err := json.MarshalIndent(j.Output, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out method for Go templates
|
2017-08-14 19:32:00 +00:00
|
|
|
func (t StdoutTemplateArray) Out() error {
|
2017-08-08 13:24:58 +00:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
2017-08-15 20:53:17 +00:00
|
|
|
if strings.HasPrefix(t.Template, "table") {
|
2017-08-08 13:24:58 +00:00
|
|
|
// replace any spaces with tabs in template so that tabwriter can align it
|
|
|
|
t.Template = strings.Replace(strings.TrimSpace(t.Template[5:]), " ", "\t", -1)
|
2017-08-15 20:53:17 +00:00
|
|
|
headerTmpl, err := template.New("header").Funcs(headerFunctions).Parse(t.Template)
|
|
|
|
if err != nil {
|
2017-08-16 02:23:40 +00:00
|
|
|
return errors.Wrapf(err, "Template parsing error")
|
2017-08-15 20:53:17 +00:00
|
|
|
}
|
2017-08-08 13:24:58 +00:00
|
|
|
err = headerTmpl.Execute(w, t.Fields)
|
2017-08-16 02:23:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 13:24:58 +00:00
|
|
|
fmt.Fprintln(w, "")
|
2017-08-15 20:53:17 +00:00
|
|
|
}
|
2017-08-08 13:24:58 +00:00
|
|
|
t.Template = strings.Replace(t.Template, " ", "\t", -1)
|
2017-08-15 20:53:17 +00:00
|
|
|
tmpl, err := template.New("image").Funcs(basicFunctions).Parse(t.Template)
|
2017-07-31 18:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Template parsing error")
|
|
|
|
}
|
|
|
|
for _, img := range t.Output {
|
2017-08-15 20:53:17 +00:00
|
|
|
basicTmpl := tmpl.Funcs(basicFunctions)
|
2017-08-08 13:24:58 +00:00
|
|
|
err = basicTmpl.Execute(w, img)
|
2017-07-31 18:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 13:24:58 +00:00
|
|
|
fmt.Fprintln(w, "")
|
2017-07-31 18:02:57 +00:00
|
|
|
}
|
2017-08-08 13:24:58 +00:00
|
|
|
return w.Flush()
|
2017-07-31 18:02:57 +00:00
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
|
|
|
|
// Out method for JSON struct
|
|
|
|
func (j JSONStruct) Out() error {
|
|
|
|
data, err := json.MarshalIndent(j.Output, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Out method for Go templates
|
|
|
|
func (t StdoutTemplate) Out() error {
|
|
|
|
tmpl, err := template.New("image").Parse(t.Template)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "template parsing error")
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(os.Stdout, t.Output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Out method for YAML
|
|
|
|
func (y YAMLStruct) Out() error {
|
|
|
|
var buf []byte
|
|
|
|
var err error
|
|
|
|
buf, err = yaml.Marshal(y.Output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(buf))
|
|
|
|
return nil
|
|
|
|
}
|