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>
This commit is contained in:
parent
5a3c168892
commit
01b71393e3
6 changed files with 155 additions and 60 deletions
54
cmd/kpod/formats/formats.go
Normal file
54
cmd/kpod/formats/formats.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
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
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue