Fix escaping

This commit is contained in:
Alex Dadgar
2017-02-15 15:14:47 -08:00
parent e9d8a6aa72
commit 03feb2efad
29 changed files with 519 additions and 228 deletions

View File

@@ -33,8 +33,13 @@ const (
var (
// jsonHandle and jsonHandlePretty are the codec handles to JSON encode
// structs. The pretty handle will add indents for easier human consumption.
jsonHandle = &codec.JsonHandle{}
jsonHandlePretty = &codec.JsonHandle{Indent: 4}
jsonHandle = &codec.JsonHandle{
HTMLCharsAsIs: true,
}
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)
// HTTPServer is used to wrap an Agent and expose it over an HTTP interface

View File

@@ -2,10 +2,18 @@ package command
import (
"bytes"
"encoding/json"
"fmt"
"io"
"text/template"
"github.com/ugorji/go/codec"
)
var (
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)
//DataFormatter is a transformer of the data.
@@ -33,12 +41,16 @@ type JSONFormat struct {
// TransformData returns JSON format string data.
func (p *JSONFormat) TransformData(data interface{}) (string, error) {
out, err := json.MarshalIndent(&data, "", " ")
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, jsonHandlePretty)
err := enc.Encode(data)
if err != nil {
return "", err
} else {
buf.Write([]byte("\n"))
}
return string(out), nil
return buf.String(), nil
}
type TemplateFormat struct {

View File

@@ -1,7 +1,6 @@
package command
import (
"encoding/json"
"fmt"
"strings"
@@ -158,12 +157,17 @@ func (c *InspectCommand) Run(args []string) int {
// Print the contents of the job
req := api.RegisterJobRequest{Job: job}
buf, err := json.MarshalIndent(req, "", " ")
f, err := DataFormat("json", "")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
c.Ui.Output(string(buf))
out, err := f.TransformData(req)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}