Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions pkg/output/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,26 @@ type Result struct {
Info string `json:"info,omitempty"`
}

// RenderJSON .
// RenderJSON renders `data` as a JSON string.
func RenderJSON(data interface{}, opts Options) string {
var jsonBytes []byte
var err error
var jsonBytes bytes.Buffer

enc := json.NewEncoder(&jsonBytes)
// Ensures characters like <>& are not converted to unicode literals.
enc.SetEscapeHTML(false)

if opts.Pretty {
jsonBytes, err = json.MarshalIndent(data, "", " ")
if err != nil {
panic(err)
}
} else {
jsonBytes, err = json.Marshal(data)
if err != nil {
panic(err)
}
enc.SetIndent("", " ")
}

if err := enc.Encode(data); err != nil {
panic(err)
}
return string(jsonBytes)

return jsonBytes.String()
}

// RenderError .
// RenderError renders an error string.
func RenderError(errorMsg string, opts Options) {
if opts.JSON {
jsonData := Result{
Expand All @@ -69,7 +70,7 @@ func RenderError(errorMsg string, opts Options) {
}
}

// RenderInfo .
// RenderInfo renders an info string.
func RenderInfo(infoMsg string, opts Options) {
if opts.JSON {
jsonData := Result{
Expand All @@ -81,7 +82,7 @@ func RenderInfo(infoMsg string, opts Options) {
}
}

// RenderResult .
// RenderResult renders a result string with optional key/value data.
func RenderResult(result Result, opts Options) string {
var out bytes.Buffer
if opts.JSON {
Expand All @@ -106,7 +107,7 @@ func RenderResult(result Result, opts Options) string {
return out.String()
}

// RenderOutput .
// RenderOutput renders tabular data.
func RenderOutput(data Table, opts Options) string {
var out bytes.Buffer
if opts.Debug {
Expand Down
12 changes: 8 additions & 4 deletions pkg/output/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func TestRenderJSON(t *testing.T) {
var testData = `Error Message`
var testSuccess = `{
"error": "Error Message"
}`
}
`
outputOptions := Options{
Header: false,
CSV: false,
Expand Down Expand Up @@ -57,7 +58,8 @@ func TestRenderError(t *testing.T) {
})
checkEqual(t, out, testSuccess, " render error stdout processing failed")

var testJson = `{"error":"Error Message"}`
var testJson = `{"error":"Error Message"}
`
outputOptions.JSON = true
out = captureStderr(func() {
RenderError(testData, outputOptions)
Expand All @@ -81,7 +83,8 @@ func TestRenderInfo(t *testing.T) {
})
checkEqual(t, out, testSuccess1, " render info stdout processing failed")

var testJson = `{"info":"Info Message"}`
var testJson = `{"info":"Info Message"}
`
outputOptions.JSON = true
out = captureStderr(func() {
RenderInfo(testData, outputOptions)
Expand Down Expand Up @@ -133,7 +136,8 @@ func TestRenderString(t *testing.T) {
output := RenderString(testData, outputOptions)
checkEqual(t, output, testSuccess1, " render string processing failed")

var testSuccess2 = `"Plain string message"`
var testSuccess2 = `"Plain string message"
`
outputOptions.JSON = true

output = RenderString(testData, outputOptions)
Expand Down
Loading