mirror of
https://github.com/containers/podman.git
synced 2025-06-20 00:51:16 +08:00
Unescape characters in inspect JSON format output
This patch changes the way the inspect command output is displayed on the screen when the format is set to JSON. Note: if the output is redirected to a file the output is *not* escaped. For example, before this commit if you run: $ sudo podman inspect --format "json" daveimg [ { ... "Author": "Dave \u003cdave@corp.io\u003e", } ... ] with this patch the output will be: [ { ... "Author": "Dave <dave@corp.io>", } ... ] Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com> Closes: #602 Approved by: mheon
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
@ -55,12 +56,23 @@ type YAMLStruct struct {
|
|||||||
Output interface{}
|
Output interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setJSONFormatEncoder(isTerminal bool, w io.Writer) *json.Encoder {
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
if isTerminal {
|
||||||
|
enc.SetEscapeHTML(false)
|
||||||
|
}
|
||||||
|
return enc
|
||||||
|
}
|
||||||
|
|
||||||
// Out method for JSON Arrays
|
// Out method for JSON Arrays
|
||||||
func (j JSONStructArray) Out() error {
|
func (j JSONStructArray) Out() error {
|
||||||
data, err := json.MarshalIndent(j.Output, "", " ")
|
buf := bytes.NewBuffer(nil)
|
||||||
if err != nil {
|
enc := setJSONFormatEncoder(terminal.IsTerminal(int(os.Stdout.Fd())), buf)
|
||||||
|
if err := enc.Encode(j.Output); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
data := buf.Bytes()
|
||||||
|
|
||||||
// JSON returns a byte array with a literal null [110 117 108 108] in it
|
// 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
|
// if it is passed empty data. We used bytes.Compare to see if that is
|
||||||
|
42
cmd/podman/formats/formats_test.go
Normal file
42
cmd/podman/formats/formats_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package formats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/projectatomic/libpod/pkg/inspect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetJSONFormatEncoder(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
name string
|
||||||
|
imageData *inspect.ImageData
|
||||||
|
expected string
|
||||||
|
isTerminal bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "HTML tags are not escaped",
|
||||||
|
imageData: &inspect.ImageData{Author: "dave <dave@corp.io>"},
|
||||||
|
expected: `"Author": "dave <dave@corp.io>"`,
|
||||||
|
isTerminal: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HTML tags are escaped",
|
||||||
|
imageData: &inspect.ImageData{Author: "dave <dave@corp.io>"},
|
||||||
|
expected: `"Author": "dave \u003cdave@corp.io\u003e"`,
|
||||||
|
isTerminal: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
enc := setJSONFormatEncoder(tc.isTerminal, buf)
|
||||||
|
if err := enc.Encode(tc.imageData); err != nil {
|
||||||
|
t.Errorf("test %#v failed encoding: %s", tc.name, err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(buf.String(), tc.expected) {
|
||||||
|
t.Errorf("test %#v expected output to contain %#v. Output:\n%v\n", tc.name, tc.expected, buf.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user