From 74e86ab06a12f9c1102060c9f6d3a913176dd8ce Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Fri, 2 Oct 2020 18:53:22 +0200 Subject: [PATCH] Fix HTML escape encoding Signed-off-by: Ulysses Souza --- cli/cmd/inspect.go | 2 +- formatter/formatter.go | 4 ++-- formatter/json.go | 22 ++++++++++------------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/cli/cmd/inspect.go b/cli/cmd/inspect.go index 20997d869..2e9647996 100644 --- a/cli/cmd/inspect.go +++ b/cli/cmd/inspect.go @@ -56,7 +56,7 @@ func runInspect(ctx context.Context, id string) error { if err != nil { return err } - fmt.Println(j) + fmt.Print(j) return nil } diff --git a/formatter/formatter.go b/formatter/formatter.go index 6686319fb..52cd9d0d5 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -38,11 +38,11 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func s := reflect.ValueOf(toJSON) for i := 0; i < s.Len(); i++ { obj := s.Index(i).Interface() - jsonLine, err := ToCompressedJSON(obj) + jsonLine, err := ToJSON(obj, "", "") if err != nil { return err } - _, _ = fmt.Fprintln(outWriter, jsonLine) + _, _ = fmt.Fprint(outWriter, jsonLine) } default: outJSON, err := ToStandardJSON(toJSON) diff --git a/formatter/json.go b/formatter/json.go index a86e3f205..afadb0c81 100644 --- a/formatter/json.go +++ b/formatter/json.go @@ -17,6 +17,7 @@ package formatter import ( + "bytes" "encoding/json" ) @@ -24,18 +25,15 @@ const standardIndentation = " " // ToStandardJSON return a string with the JSON representation of the interface{} func ToStandardJSON(i interface{}) (string, error) { - b, err := json.MarshalIndent(i, "", standardIndentation) - if err != nil { - return "", err - } - return string(b), nil + return ToJSON(i, "", standardIndentation) } -// ToCompressedJSON return a string with the JSON representation of the interface{} -func ToCompressedJSON(i interface{}) (string, error) { - b, err := json.Marshal(i) - if err != nil { - return "", err - } - return string(b), nil +// ToJSON return a string with the JSON representation of the interface{} +func ToJSON(i interface{}, prefix string, indentation string) (string, error) { + buffer := &bytes.Buffer{} + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.SetIndent(prefix, indentation) + err := encoder.Encode(i) + return buffer.String(), err }