diff --git a/cmd/globals.go b/cmd/globals.go index df3c7d7f..1fc149c3 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -57,6 +57,7 @@ const ( var ( globalQuiet = false // Quiet flag set via command line globalJSON = false // Json flag set via command line + globalJSONLine = false // Print json as single line. globalDebug = false // Debug flag set via command line globalNoColor = false // No Color flag set via command line globalInsecure = false // Insecure flag set via command line @@ -76,8 +77,9 @@ var ( func setGlobals(quiet, debug, json, noColor, insecure bool) { globalQuiet = globalQuiet || quiet globalDebug = globalDebug || debug + globalJSONLine = !isTerminal() && json globalJSON = globalJSON || json - globalNoColor = globalNoColor || noColor + globalNoColor = globalNoColor || noColor || globalJSONLine globalInsecure = globalInsecure || insecure // Disable colorified messages if requested. diff --git a/cmd/print.go b/cmd/print.go index 0705f009..fe0ebf5d 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -18,6 +18,10 @@ package cmd import ( + "bytes" + "encoding/json" + "strings" + "github.com/minio/pkg/console" ) @@ -34,6 +38,13 @@ func printMsg(msg message) { msgStr = msg.String() } else { msgStr = msg.JSON() + if globalJSONLine && strings.ContainsRune(msgStr, '\n') { + // Reformat. + var dst bytes.Buffer + if err := json.Compact(&dst, []byte(msgStr)); err == nil { + msgStr = dst.String() + } + } } console.Println(msgStr) }