Merge pull request #886 from baude/kpod_json

Return Valid JSON for empty data
This commit is contained in:
Daniel J Walsh 2017-10-11 16:26:23 -04:00 committed by GitHub
commit d7cbdfce76
2 changed files with 14 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import (
"text/tabwriter" "text/tabwriter"
"text/template" "text/template"
"bytes"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -59,6 +60,16 @@ func (j JSONStructArray) Out() error {
if err != nil { if err != nil {
return err return err
} }
// JSON returns a byte array with a literal null [110 117 108 108] in it
// if it is passed empty data. We used bytes.Compare to see if that is
// the case.
if diff := bytes.Compare(data, []byte("null")); diff == 0 {
data = []byte("[]")
}
// If the we did get NULL back, we should spit out {} which is
// at least valid JSON for the consumer.
fmt.Printf("%s\n", data) fmt.Printf("%s\n", data)
return nil return nil
} }

View file

@ -398,7 +398,9 @@ func getJSONOutput(containers []*libkpod.ContainerData, nSpace bool) (psOutput [
func generatePsOutput(containers []*libkpod.ContainerData, server *libkpod.ContainerServer, opts psOptions) error { func generatePsOutput(containers []*libkpod.ContainerData, server *libkpod.ContainerServer, opts psOptions) error {
containersOutput := getContainers(containers, opts) containersOutput := getContainers(containers, opts)
if len(containersOutput) == 0 { // In the case of JSON, we want to continue so we at least pass
// {} --valid JSON-- to the consumer
if len(containersOutput) == 0 && opts.format != formats.JSONString {
return nil return nil
} }