2017-07-31 18:02:57 +00:00
|
|
|
package formats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-08-14 19:32:00 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2017-07-31 18:02:57 +00:00
|
|
|
"os"
|
2017-08-15 20:53:17 +00:00
|
|
|
"strings"
|
2017-07-31 18:02:57 +00:00
|
|
|
"text/template"
|
2017-08-15 20:53:17 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-07-31 18:02:57 +00:00
|
|
|
)
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
// JSONString const to save on duplicate variable names
|
|
|
|
const JSONString string = "json"
|
|
|
|
|
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-15 20:53:17 +00:00
|
|
|
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 {
|
2017-08-16 02:23:40 +00:00
|
|
|
return errors.Wrapf(err, "Template parsing error")
|
2017-08-15 20:53:17 +00:00
|
|
|
}
|
|
|
|
err = headerTmpl.Execute(os.Stdout, t.Fields)
|
2017-08-16 02:23:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-15 20:53:17 +00:00
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
err = basicTmpl.Execute(os.Stdout, img)
|
2017-07-31 18:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|