cri-o/cmd/kpod/formats/formats.go
baude 01b71393e3 cmd/kpod/images.go: Add structured format ouput
For kpod images, we need to output in JSON format so that consumers
(programatic) have structured input to work with.

kpod images --format json

Signed-off-by: baude <bbaude@redhat.com>
2017-08-12 19:09:49 -05:00

54 lines
890 B
Go

package formats
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"os"
"text/template"
)
// Writer interface for outputs
type Writer interface {
Out() error
}
// JSONstruct for JSON output
type JSONstruct struct {
Output []interface{}
}
// StdoutTemplate for Go template output
type StdoutTemplate struct {
Output []interface{}
Template string
}
// Out method for JSON
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")
}
for _, img := range t.Output {
err = tmpl.Execute(os.Stdout, img)
if err != nil {
return err
}
fmt.Println()
}
return nil
}