Fix HTML escape encoding

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-10-02 18:53:22 +02:00
parent 83f21b9293
commit 74e86ab06a
3 changed files with 13 additions and 15 deletions

View File

@ -56,7 +56,7 @@ func runInspect(ctx context.Context, id string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Println(j) fmt.Print(j)
return nil return nil
} }

View File

@ -38,11 +38,11 @@ func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func
s := reflect.ValueOf(toJSON) s := reflect.ValueOf(toJSON)
for i := 0; i < s.Len(); i++ { for i := 0; i < s.Len(); i++ {
obj := s.Index(i).Interface() obj := s.Index(i).Interface()
jsonLine, err := ToCompressedJSON(obj) jsonLine, err := ToJSON(obj, "", "")
if err != nil { if err != nil {
return err return err
} }
_, _ = fmt.Fprintln(outWriter, jsonLine) _, _ = fmt.Fprint(outWriter, jsonLine)
} }
default: default:
outJSON, err := ToStandardJSON(toJSON) outJSON, err := ToStandardJSON(toJSON)

View File

@ -17,6 +17,7 @@
package formatter package formatter
import ( import (
"bytes"
"encoding/json" "encoding/json"
) )
@ -24,18 +25,15 @@ const standardIndentation = " "
// ToStandardJSON return a string with the JSON representation of the interface{} // ToStandardJSON return a string with the JSON representation of the interface{}
func ToStandardJSON(i interface{}) (string, error) { func ToStandardJSON(i interface{}) (string, error) {
b, err := json.MarshalIndent(i, "", standardIndentation) return ToJSON(i, "", standardIndentation)
if err != nil {
return "", err
}
return string(b), nil
} }
// ToCompressedJSON return a string with the JSON representation of the interface{} // ToJSON return a string with the JSON representation of the interface{}
func ToCompressedJSON(i interface{}) (string, error) { func ToJSON(i interface{}, prefix string, indentation string) (string, error) {
b, err := json.Marshal(i) buffer := &bytes.Buffer{}
if err != nil { encoder := json.NewEncoder(buffer)
return "", err encoder.SetEscapeHTML(false)
} encoder.SetIndent(prefix, indentation)
return string(b), nil err := encoder.Encode(i)
return buffer.String(), err
} }