diff --git a/cmd/accounting-reader.go b/cmd/accounting-reader.go index 92630598..f69c11f6 100644 --- a/cmd/accounting-reader.go +++ b/cmd/accounting-reader.go @@ -17,12 +17,14 @@ package cmd import ( + "encoding/json" "fmt" "sync" "sync/atomic" "time" "github.com/cheggaaa/pb" + "github.com/minio/mc/pkg/probe" ) // accounter keeps tabs of ongoing data transfer information. @@ -78,9 +80,18 @@ func (a *accounter) writer() { // accountStat cantainer for current stats captured. type accountStat struct { - Total int64 - Transferred int64 - Speed float64 + Status string `json:"status"` + Total int64 `json:"total"` + Transferred int64 `json:"transferred"` + Speed float64 `json:"speed"` +} + +func (c accountStat) JSON() string { + c.Status = "success" + accountMessageBytes, e := json.MarshalIndent(c, "", " ") + fatalIf(probe.NewError(e), "Failed to marshal account stat.") + + return string(accountMessageBytes) } func (c accountStat) String() string { diff --git a/cmd/admin-config-get.go b/cmd/admin-config-get.go index 4642caa9..9161cf72 100644 --- a/cmd/admin-config-get.go +++ b/cmd/admin-config-get.go @@ -17,10 +17,8 @@ package cmd import ( - "encoding/json" - "strings" - "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" ) @@ -48,23 +46,25 @@ EXAMPLES: // configGetMessage container to hold locks information. type configGetMessage struct { - Status string `json:"status"` - Config string `json:"config"` + Status string `json:"status"` + Config map[string]interface{} `json:"config"` } // String colorized service status message. func (u configGetMessage) String() string { - return string(u.Config) + config, e := json.MarshalIndent(u.Config, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(config) } // JSON jsonified service status Message message. func (u configGetMessage) JSON() string { u.Status = "success" - statusJSONBytes, e := json.MarshalIndent(u, "", "\t") + statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") - // Remove \n and \t from u.Config which holds the config data - return strings.NewReplacer(`\n`, "", `\t`, "").Replace(string(statusJSONBytes)) + return string(statusJSONBytes) } // checkAdminConfigGetSyntax - validate all the passed arguments @@ -90,8 +90,14 @@ func mainAdminConfigGet(ctx *cli.Context) error { c, e := client.GetConfig() fatalIf(probe.NewError(e), "Cannot get server configuration file.") + config := map[string]interface{}{} + e = json.Unmarshal(c, &config) + fatalIf(probe.NewError(e), "Cannot unmarshal server configuration file.") + // Print - printMsg(configGetMessage{Config: string(c)}) + printMsg(configGetMessage{ + Config: config, + }) return nil } diff --git a/cmd/admin-config-set.go b/cmd/admin-config-set.go index c761d7cc..6a19e3ac 100644 --- a/cmd/admin-config-set.go +++ b/cmd/admin-config-set.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "os" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -76,7 +76,7 @@ func (u configSetMessage) JSON() string { } else { u.Status = "error" } - statusJSONBytes, e := json.Marshal(u) + statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(statusJSONBytes) diff --git a/cmd/admin-heal-ui.go b/cmd/admin-heal-ui.go index b097d3bd..9aa3adfe 100644 --- a/cmd/admin-heal-ui.go +++ b/cmd/admin-heal-ui.go @@ -17,7 +17,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "math" @@ -28,6 +27,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/fatih/color" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -299,7 +299,7 @@ func (ui *uiData) printItemsJSON(s *madmin.HealTaskStatus) (err error) { if err != nil { return err } - jsonBytes, err := json.Marshal(r) + jsonBytes, err := json.MarshalIndent(r, "", " ") fatalIf(probe.NewError(err), "Unable to marshal to JSON.") console.Println(string(jsonBytes)) } @@ -329,7 +329,7 @@ func (ui *uiData) printStatsJSON(s *madmin.HealTaskStatus) { summary.Size = ui.BytesScanned summary.ElapsedTime = int64(ui.HealDuration.Round(time.Second).Seconds()) - jBytes, err := json.Marshal(summary) + jBytes, err := json.MarshalIndent(summary, "", " ") fatalIf(probe.NewError(err), "Unable to marshal to JSON.") console.Println(string(jBytes)) } diff --git a/cmd/admin-heal.go b/cmd/admin-heal.go index 33f7773a..c4b66ff5 100644 --- a/cmd/admin-heal.go +++ b/cmd/admin-heal.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "path/filepath" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -104,7 +104,7 @@ func (s stopHealMessage) String() string { // JSON jsonified stop heal message. func (s stopHealMessage) JSON() string { - stopHealJSONBytes, e := json.Marshal(s) + stopHealJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(stopHealJSONBytes) @@ -169,7 +169,7 @@ func mainAdminHeal(ctx *cli.Context) error { res, e := ui.DisplayAndFollowHealStatus(aliasedURL) if e != nil { if res.FailureDetail != "" { - data, _ := json.Marshal(res) + data, _ := json.MarshalIndent(res, "", " ") traceStr := string(data) errorIf(probe.NewError(e).Trace(aliasedURL, traceStr), "Unable to display heal status.") } else { diff --git a/cmd/admin-info.go b/cmd/admin-info.go index a6d37427..c9a232d9 100644 --- a/cmd/admin-info.go +++ b/cmd/admin-info.go @@ -17,7 +17,6 @@ package cmd import ( - "encoding/json" "fmt" "net/url" "time" @@ -25,6 +24,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -170,7 +170,7 @@ func (u infoMessage) String() (msg string) { // JSON jsonified service status Message message. func (u infoMessage) JSON() string { u.Status = "success" - statusJSONBytes, e := json.Marshal(u) + statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(statusJSONBytes) diff --git a/cmd/admin-policy-add.go b/cmd/admin-policy-add.go index 456e7ed2..22962eef 100644 --- a/cmd/admin-policy-add.go +++ b/cmd/admin-policy-add.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "io/ioutil" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -89,7 +89,7 @@ func (u userPolicyMessage) String() string { func (u userPolicyMessage) JSON() string { u.Status = "success" - jsonMessageBytes, e := json.Marshal(u) + jsonMessageBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) diff --git a/cmd/admin-service-restart.go b/cmd/admin-service-restart.go index a474b028..09f39a57 100644 --- a/cmd/admin-service-restart.go +++ b/cmd/admin-service-restart.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "time" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -62,7 +62,7 @@ func (s serviceRestartCommand) String() string { // JSON jsonified service restart command message. func (s serviceRestartCommand) JSON() string { - serviceRestartJSONBytes, e := json.Marshal(s) + serviceRestartJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(serviceRestartJSONBytes) @@ -85,7 +85,7 @@ func (s serviceRestartMessage) String() string { // JSON jsonified service restart message. func (s serviceRestartMessage) JSON() string { - serviceRestartJSONBytes, e := json.Marshal(s) + serviceRestartJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(serviceRestartJSONBytes) diff --git a/cmd/admin-service-status.go b/cmd/admin-service-status.go index 87723504..4caf7ef9 100644 --- a/cmd/admin-service-status.go +++ b/cmd/admin-service-status.go @@ -17,13 +17,13 @@ package cmd import ( - "encoding/json" "fmt" "net/url" "time" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -83,7 +83,7 @@ func (u serviceStatusMessage) JSON() string { u.Status = "failure" } - statusJSONBytes, e := json.Marshal(u) + statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(statusJSONBytes) diff --git a/cmd/admin-service-stop.go b/cmd/admin-service-stop.go index 5a17c99c..c09ee26c 100644 --- a/cmd/admin-service-stop.go +++ b/cmd/admin-service-stop.go @@ -17,10 +17,9 @@ package cmd import ( - "encoding/json" - "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -61,7 +60,7 @@ func (s serviceStopMessage) String() string { // JSON jsonified make bucket message. func (s serviceStopMessage) JSON() string { - serviceStopJSONBytes, e := json.Marshal(s) + serviceStopJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(serviceStopJSONBytes) diff --git a/cmd/admin-top-locks.go b/cmd/admin-top-locks.go index 52029bbd..d4477f2f 100644 --- a/cmd/admin-top-locks.go +++ b/cmd/admin-top-locks.go @@ -17,12 +17,12 @@ package cmd import ( - "encoding/json" "fmt" "time" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/madmin" @@ -90,7 +90,7 @@ func (u lockMessage) String() string { // JSON jsonified top oldest locks message. func (u lockMessage) JSON() string { u.Status = "success" - statusJSONBytes, e := json.Marshal(u) + statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(statusJSONBytes) } diff --git a/cmd/admin-user-add.go b/cmd/admin-user-add.go index 182aee52..fe2b1e59 100644 --- a/cmd/admin-user-add.go +++ b/cmd/admin-user-add.go @@ -17,10 +17,9 @@ package cmd import ( - "encoding/json" - "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -97,7 +96,7 @@ func (u userMessage) String() string { func (u userMessage) JSON() string { u.Status = "success" - jsonMessageBytes, e := json.Marshal(u) + jsonMessageBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index fe080a6b..2afa6525 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "crypto/tls" - "encoding/json" "hash/fnv" "io" "net" @@ -34,6 +33,7 @@ import ( "sync" "time" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/httptracer" "github.com/minio/mc/pkg/probe" minio "github.com/minio/minio-go" @@ -983,7 +983,7 @@ func (c *s3Client) SetAccess(bucketPolicy string, isJSON bool) *probe.Error { } return nil } - policyB, e := json.Marshal(p) + policyB, e := json.MarshalIndent(p, "", " ") if e != nil { return probe.NewError(e) } diff --git a/cmd/config-host.go b/cmd/config-host.go index f52387ff..4a7f3bea 100644 --- a/cmd/config-host.go +++ b/cmd/config-host.go @@ -17,9 +17,8 @@ package cmd import ( - "encoding/json" - "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -86,7 +85,7 @@ func (h hostMessage) String() string { // JSON jsonified host message func (h hostMessage) JSON() string { h.Status = "success" - jsonMessageBytes, e := json.Marshal(h) + jsonMessageBytes, e := json.MarshalIndent(h, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 92806b07..b79456cd 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -128,7 +128,7 @@ func (c copyMessage) String() string { // JSON jsonified copy message func (c copyMessage) JSON() string { c.Status = "success" - copyMessageBytes, e := json.Marshal(c) + copyMessageBytes, e := json.MarshalIndent(c, "", " ") fatalIf(probe.NewError(e), "Failed to marshal copy message.") return string(copyMessageBytes) @@ -347,7 +347,8 @@ func doCopySession(session *sessionV8, encKeyDB map[string][]prefixSSEPair) erro } var cpURLs URLs - // Unmarshal copyURLs from each line. + // Unmarshal copyURLs from each line. This expects each line to be + // an entire JSON object. if e := json.Unmarshal([]byte(urlScanner.Text()), &cpURLs); e != nil { errorIf(probe.NewError(e), "Unable to unmarshal %s", urlScanner.Text()) continue @@ -431,7 +432,7 @@ loop: } } else { if accntReader, ok := pg.(*accounter); ok { - console.Println(console.Colorize("Copy", accntReader.Stat().String())) + printMsg(accntReader.Stat()) } } diff --git a/cmd/diff-main.go b/cmd/diff-main.go index a2c0af80..2a0dcc55 100644 --- a/cmd/diff-main.go +++ b/cmd/diff-main.go @@ -17,12 +17,12 @@ package cmd import ( - "encoding/json" "fmt" "strings" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -102,7 +102,7 @@ func (d diffMessage) String() string { // JSON jsonified diff message func (d diffMessage) JSON() string { d.Status = "success" - diffJSONBytes, e := json.Marshal(d) + diffJSONBytes, e := json.MarshalIndent(d, "", " ") fatalIf(probe.NewError(e), "Unable to marshal diff message `"+d.FirstURL+"`, `"+d.SecondURL+"` and `"+string(d.Diff)+"`.") return string(diffJSONBytes) diff --git a/cmd/error.go b/cmd/error.go index fd51dbad..be619303 100644 --- a/cmd/error.go +++ b/cmd/error.go @@ -17,12 +17,12 @@ package cmd import ( - "encoding/json" "fmt" "strings" "unicode" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -64,13 +64,13 @@ func fatal(err *probe.Error, msg string, data ...interface{}) { if globalDebug { errorMsg.CallTrace = err.CallTrace } - json, e := json.Marshal(struct { + json, e := json.MarshalIndent(struct { Status string `json:"status"` Error errorMessage `json:"error"` }{ Status: "error", Error: errorMsg, - }) + }, "", " ") if e != nil { console.Fatalln(probe.NewError(e)) } @@ -135,13 +135,13 @@ func errorIf(err *probe.Error, msg string, data ...interface{}) { if globalDebug { errorMsg.CallTrace = err.CallTrace } - json, e := json.Marshal(struct { + json, e := json.MarshalIndent(struct { Status string `json:"status"` Error errorMessage `json:"error"` }{ Status: "error", Error: errorMsg, - }) + }, "", " ") if e != nil { console.Fatalln(probe.NewError(e)) } diff --git a/cmd/event-add.go b/cmd/event-add.go index ac90671b..b5573652 100644 --- a/cmd/event-add.go +++ b/cmd/event-add.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "strings" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -88,7 +88,7 @@ type eventAddMessage struct { // JSON jsonified update message. func (u eventAddMessage) JSON() string { u.Status = "success" - eventAddMessageJSONBytes, e := json.Marshal(u) + eventAddMessageJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(eventAddMessageJSONBytes) } diff --git a/cmd/event-list.go b/cmd/event-list.go index d438d004..f23e94e9 100644 --- a/cmd/event-list.go +++ b/cmd/event-list.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "fmt" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -74,7 +74,7 @@ type eventListMessage struct { func (u eventListMessage) JSON() string { u.Status = "success" - eventListMessageJSONBytes, e := json.Marshal(u) + eventListMessageJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(eventListMessageJSONBytes) } diff --git a/cmd/event-remove.go b/cmd/event-remove.go index abbc1f4d..8f106bce 100644 --- a/cmd/event-remove.go +++ b/cmd/event-remove.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "errors" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -79,7 +79,7 @@ type eventRemoveMessage struct { // JSON jsonified remove message. func (u eventRemoveMessage) JSON() string { u.Status = "success" - eventRemoveMessageJSONBytes, e := json.Marshal(u) + eventRemoveMessageJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(eventRemoveMessageJSONBytes) } diff --git a/cmd/ls.go b/cmd/ls.go index dca3de28..67d435b4 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -17,7 +17,6 @@ package cmd import ( - "encoding/json" "fmt" "path/filepath" "runtime" @@ -25,6 +24,7 @@ import ( "time" "github.com/dustin/go-humanize" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -60,7 +60,7 @@ func (c contentMessage) String() string { // JSON jsonified content message. func (c contentMessage) JSON() string { c.Status = "success" - jsonMessageBytes, e := json.Marshal(c) + jsonMessageBytes, e := json.MarshalIndent(c, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) diff --git a/cmd/mb-main.go b/cmd/mb-main.go index 04003397..991f32f6 100644 --- a/cmd/mb-main.go +++ b/cmd/mb-main.go @@ -17,10 +17,9 @@ package cmd import ( - "encoding/json" - "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -88,7 +87,7 @@ func (s makeBucketMessage) String() string { // JSON jsonified make bucket message. func (s makeBucketMessage) JSON() string { - makeBucketJSONBytes, e := json.Marshal(s) + makeBucketJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(makeBucketJSONBytes) diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index a9ed3a3a..1ccd6841 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -18,7 +18,6 @@ package cmd import ( "context" - "encoding/json" "fmt" "os" "path" @@ -30,6 +29,7 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -210,7 +210,7 @@ func (m mirrorMessage) String() string { // JSON jsonified mirror message func (m mirrorMessage) JSON() string { m.Status = "success" - mirrorMessageBytes, e := json.Marshal(m) + mirrorMessageBytes, e := json.MarshalIndent(m, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(mirrorMessageBytes) diff --git a/cmd/policy-main.go b/cmd/policy-main.go index b4ef95e3..818bb26d 100644 --- a/cmd/policy-main.go +++ b/cmd/policy-main.go @@ -18,7 +18,6 @@ package cmd import ( "bytes" - "encoding/json" "io" "net/url" "os" @@ -27,6 +26,7 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -106,7 +106,7 @@ func (s policyRules) String() string { // JSON jsonified policy message. func (s policyRules) JSON() string { - policyJSONBytes, e := json.Marshal(s) + policyJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(policyJSONBytes) } @@ -139,7 +139,7 @@ func (s policyMessage) String() string { // JSON jsonified policy message. func (s policyMessage) JSON() string { - policyJSONBytes, e := json.Marshal(s) + policyJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(policyJSONBytes) @@ -158,7 +158,7 @@ func (s policyLinksMessage) String() string { // JSON jsonified policy message. func (s policyLinksMessage) JSON() string { - policyJSONBytes, e := json.Marshal(s) + policyJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(policyJSONBytes) diff --git a/cmd/rm-main.go b/cmd/rm-main.go index 274c9696..5cae9344 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -18,7 +18,6 @@ package cmd import ( "bufio" - "encoding/json" "fmt" "os" "path/filepath" @@ -27,6 +26,7 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -134,7 +134,7 @@ func (r rmMessage) String() string { // JSON'ified message for scripting. func (r rmMessage) JSON() string { - msgBytes, e := json.Marshal(r) + msgBytes, e := json.MarshalIndent(r, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(msgBytes) } diff --git a/cmd/session-clear.go b/cmd/session-clear.go index 65c46714..ac66b636 100644 --- a/cmd/session-clear.go +++ b/cmd/session-clear.go @@ -17,10 +17,9 @@ package cmd import ( - "encoding/json" - "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -82,7 +81,7 @@ func (c clearSessionMessage) String() string { // JSON jsonified clear session message. func (c clearSessionMessage) JSON() string { - clearSessionJSONBytes, e := json.Marshal(c) + clearSessionJSONBytes, e := json.MarshalIndent(c, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(clearSessionJSONBytes) diff --git a/cmd/session-v8.go b/cmd/session-v8.go index 6b98a978..308a0dbb 100644 --- a/cmd/session-v8.go +++ b/cmd/session-v8.go @@ -19,7 +19,6 @@ package cmd import ( - "encoding/json" "errors" "fmt" "io" @@ -28,6 +27,7 @@ import ( "sync" "time" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/quick" @@ -98,7 +98,7 @@ func (s sessionV8) JSON() string { CommandArgs: s.Header.CommandArgs, } sessionMsg.Status = "success" - sessionBytes, e := json.Marshal(sessionMsg) + sessionBytes, e := json.MarshalIndent(sessionMsg, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(sessionBytes) diff --git a/cmd/share.go b/cmd/share.go index 6731c2d3..71fe7fd0 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -18,7 +18,6 @@ package cmd import ( "bytes" - "encoding/json" "fmt" "os" "path/filepath" @@ -27,6 +26,7 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -79,7 +79,7 @@ func (s shareMesssage) String() string { // JSON - JSONified message for scripting. func (s shareMesssage) JSON() string { s.Status = "success" - shareMessageBytes, e := json.Marshal(s) + shareMessageBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Failed to marshal into JSON.") // JSON encoding escapes ampersand into its unicode character diff --git a/cmd/stat.go b/cmd/stat.go index 93ff72d0..a38bedcd 100644 --- a/cmd/stat.go +++ b/cmd/stat.go @@ -17,13 +17,13 @@ package cmd import ( - "encoding/json" "fmt" "path/filepath" "strings" "time" humanize "github.com/dustin/go-humanize" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -82,7 +82,7 @@ func printStat(stat statMessage) { // JSON jsonified content message. func (c statMessage) JSON() string { c.Status = "success" - jsonMessageBytes, e := json.Marshal(c) + jsonMessageBytes, e := json.MarshalIndent(c, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) diff --git a/cmd/update-main.go b/cmd/update-main.go index 83044f63..5a560cc1 100644 --- a/cmd/update-main.go +++ b/cmd/update-main.go @@ -19,7 +19,6 @@ package cmd import ( "crypto" "encoding/hex" - "encoding/json" "fmt" "io/ioutil" "net/http" @@ -33,6 +32,7 @@ import ( "github.com/inconshreveable/go-update" isatty "github.com/mattn/go-isatty" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" _ "github.com/minio/sha256-simd" // Needed for sha256 hash verifier. "github.com/segmentio/go-prompt" @@ -450,7 +450,7 @@ func (s updateMessage) String() string { // JSON jsonified make bucket message. func (s updateMessage) JSON() string { - updateJSONBytes, e := json.Marshal(s) + updateJSONBytes, e := json.MarshalIndent(s, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(updateJSONBytes) diff --git a/cmd/version-main.go b/cmd/version-main.go index 70440f32..b159692c 100644 --- a/cmd/version-main.go +++ b/cmd/version-main.go @@ -17,11 +17,11 @@ package cmd import ( - "encoding/json" "fmt" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -78,7 +78,7 @@ func (v versionMessage) String() string { // JSON'ified message for scripting. func (v versionMessage) JSON() string { v.Status = "success" - msgBytes, e := json.Marshal(v) + msgBytes, e := json.MarshalIndent(v, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(msgBytes) } diff --git a/cmd/watch-main.go b/cmd/watch-main.go index 3426e6ca..fadc222a 100644 --- a/cmd/watch-main.go +++ b/cmd/watch-main.go @@ -17,7 +17,6 @@ package cmd import ( - "encoding/json" "fmt" "os" "strings" @@ -27,6 +26,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/fatih/color" "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/probe" ) @@ -111,7 +111,7 @@ type watchMessage struct { func (u watchMessage) JSON() string { u.Status = "success" - watchMessageJSONBytes, e := json.Marshal(u) + watchMessageJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(watchMessageJSONBytes) } diff --git a/pkg/colorjson/decode.go b/pkg/colorjson/decode.go index cfd46d9a..4953a03f 100644 --- a/pkg/colorjson/decode.go +++ b/pkg/colorjson/decode.go @@ -106,7 +106,17 @@ import ( // character U+FFFD. // func Unmarshal(data []byte, v interface{}) error { - panic("unmarshalling is not supported by colorjson") + // Check for well-formedness. + // Avoids filling out half a data structure + // before discovering a JSON syntax error. + var d decodeState + err := checkValid(data, &d.scan) + if err != nil { + return err + } + + d.init(data) + return d.unmarshal(v) } // Unmarshaler is the interface implemented by types diff --git a/pkg/colorjson/encode.go b/pkg/colorjson/encode.go index 78b9a442..746b615b 100644 --- a/pkg/colorjson/encode.go +++ b/pkg/colorjson/encode.go @@ -28,6 +28,7 @@ import ( "encoding/base64" "fmt" "math" + "os" "reflect" "sort" "strconv" @@ -36,6 +37,7 @@ import ( "unicode" "unicode/utf8" + "github.com/mattn/go-isatty" "github.com/minio/mc/pkg/console" ) @@ -194,6 +196,9 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { if err != nil { return nil, err } + if !isatty.IsTerminal(os.Stdout.Fd()) { + return b, err + } var buf bytes.Buffer err = Indent(&buf, b, prefix, indent) if err != nil { @@ -622,7 +627,7 @@ func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) { return } if opts.quoted { - sb, err := Marshal(console.Colorize(jsonString, v.String())) + sb, err := Marshal(v.String()) if err != nil { e.error(err) } @@ -882,7 +887,7 @@ type reflectWithString struct { func (w *reflectWithString) resolve() error { if w.v.Kind() == reflect.String { - w.s = console.Colorize(jsonString, w.v.String()) + w.s = w.v.String() return nil } if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { @@ -892,10 +897,10 @@ func (w *reflectWithString) resolve() error { } switch w.v.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - w.s = console.Colorize(jsonNum, strconv.FormatInt(w.v.Int(), 10)) + w.s = strconv.FormatInt(w.v.Int(), 10) return nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - w.s = console.Colorize(jsonNum, strconv.FormatUint(w.v.Uint(), 10)) + w.s = strconv.FormatUint(w.v.Uint(), 10) return nil } panic("unexpected map key type")