From d5dd4fb5bd0f6983090da224911fb4bd794b7943 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 18 Nov 2015 00:17:24 -0800 Subject: [PATCH] json: JSON messages now have proper status field for all messages Fixes - #1369 --- config-alias-main.go | 8 +++-- config-host-main.go | 2 ++ cp-main.go | 2 ++ diff-main.go | 2 ++ error.go | 79 ++++++++++++++++---------------------------- ls.go | 2 ++ mirror-main.go | 2 ++ rm-main.go | 7 ++-- session-v3.go | 10 +++--- share.go | 2 ++ update-main.go | 3 +- version-main.go | 2 ++ 12 files changed, 60 insertions(+), 61 deletions(-) diff --git a/config-alias-main.go b/config-alias-main.go index b9f4282d..101771e8 100644 --- a/config-alias-main.go +++ b/config-alias-main.go @@ -74,9 +74,10 @@ EXAMPLES: // aliasMessage container for content message structure type aliasMessage struct { - op string - Alias string `json:"alias"` - URL string `json:"url,omitempty"` + op string + Status string `json:"status"` + Alias string `json:"alias"` + URL string `json:"url,omitempty"` } // String colorized alias message @@ -98,6 +99,7 @@ func (a aliasMessage) String() string { // JSON jsonified alias message func (a aliasMessage) JSON() string { + a.Status = "success" jsonMessageBytes, e := json.Marshal(a) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") diff --git a/config-host-main.go b/config-host-main.go index ed1819af..7a857c85 100644 --- a/config-host-main.go +++ b/config-host-main.go @@ -62,6 +62,7 @@ EXAMPLES: // hostMessage container for content message structure type hostMessage struct { op string + Status string `json:"status"` Host string `json:"host"` AccessKeyID string `json:"accessKeyId,omitempty"` SecretAccessKey string `json:"secretAccessKey,omitempty"` @@ -91,6 +92,7 @@ func (a hostMessage) String() string { // JSON jsonified host message func (a hostMessage) JSON() string { + a.Status = "success" jsonMessageBytes, e := json.Marshal(a) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") diff --git a/cp-main.go b/cp-main.go index 81a98840..0c00c3ff 100644 --- a/cp-main.go +++ b/cp-main.go @@ -81,6 +81,7 @@ EXAMPLES: // copyMessage container for file copy messages type copyMessage struct { + Status string `json:"status"` Source string `json:"source"` Target string `json:"target"` Length int64 `json:"length"` @@ -93,6 +94,7 @@ func (c copyMessage) String() string { // JSON jsonified copy message func (c copyMessage) JSON() string { + c.Status = "success" copyMessageBytes, err := json.Marshal(c) fatalIf(probe.NewError(err), "Failed to marshal copy message.") diff --git a/diff-main.go b/diff-main.go index 68a01597..4a899a7d 100644 --- a/diff-main.go +++ b/diff-main.go @@ -77,6 +77,7 @@ func checkDiffSyntax(ctx *cli.Context) { // diffMessage json container for diff messages type diffMessage struct { + Status string `json:"status"` FirstURL string `json:"first"` SecondURL string `json:"second"` Diff string `json:"diff"` @@ -106,6 +107,7 @@ func (d diffMessage) String() string { // JSON jsonified diff message func (d diffMessage) JSON() string { + d.Status = "success" diffJSONBytes, err := json.Marshal(d) fatalIf(probe.NewError(err), "Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+d.Diff+"’.") diff --git a/error.go b/error.go index f0443145..3171dd3d 100644 --- a/error.go +++ b/error.go @@ -24,71 +24,45 @@ import ( "github.com/minio/minio-xl/pkg/probe" ) -// ErrorMessage container for error messages -type ErrorMessage struct { +// causeMessage container for golang error messages +type causeMessage struct { + Message string `json:"message"` + Error error `json:"error"` +} + +// errorMessage container for error messages +type errorMessage struct { Message string `json:"message"` - Cause error `json:"cause"` + Cause causeMessage `json:"cause"` Type string `json:"type"` CallTrace []probe.TracePoint `json:"trace,omitempty"` SysInfo map[string]string `json:"sysinfo"` } -// fatalIfMultiple wrapper function which takes error message map and selectively prints stack frames if available on debug -// fatalIfMultiple is implemented to handle multiple argument validation. -/* -func fatalIfMultiple(errMsgMap map[string]*probe.Error) { - if errMsgMap == nil { - return - } - for msg, err := range errMsgMap { - if globalJSONFlag { - errorMessage := ErrorMessage{ - Message: msg, - Type: "error", - Cause: err.ToGoError(), - SysInfo: err.SysInfo, - } - if globalDebugFlag { - errorMessage.CallTrace = err.CallTrace - } - json, err := json.Marshal(struct { - Error ErrorMessage `json:"error"` - }{ - Error: errorMessage, - }) - if err != nil { - console.Fatalln(probe.NewError(err)) - } - console.Println(string(json)) - } - if !globalDebugFlag { - console.Errorln(fmt.Sprintf("%s %s", msg, err.ToGoError())) - } - console.Errorln(fmt.Sprintf("%s %s", msg, err)) - } - console.Fatalln() -} -*/ - // fatalIf wrapper function which takes error and selectively prints stack frames if available on debug func fatalIf(err *probe.Error, msg string) { if err == nil { return } if globalJSONFlag { - errorMessage := ErrorMessage{ + errorMsg := errorMessage{ Message: msg, Type: "fatal", - Cause: err.ToGoError(), + Cause: causeMessage{ + Message: err.ToGoError().Error(), + Error: err.ToGoError(), + }, SysInfo: err.SysInfo, } if globalDebugFlag { - errorMessage.CallTrace = err.CallTrace + errorMsg.CallTrace = err.CallTrace } json, err := json.Marshal(struct { - Error ErrorMessage `json:"error"` + Status string `json:"status"` + Error errorMessage `json:"error"` }{ - Error: errorMessage, + Status: "error", + Error: errorMsg, }) if err != nil { console.Fatalln(probe.NewError(err)) @@ -108,19 +82,24 @@ func errorIf(err *probe.Error, msg string) { return } if globalJSONFlag { - errorMessage := ErrorMessage{ + errorMsg := errorMessage{ Message: msg, Type: "error", - Cause: err.ToGoError(), + Cause: causeMessage{ + Message: err.ToGoError().Error(), + Error: err.ToGoError(), + }, SysInfo: err.SysInfo, } if globalDebugFlag { - errorMessage.CallTrace = err.CallTrace + errorMsg.CallTrace = err.CallTrace } json, err := json.Marshal(struct { - Error ErrorMessage `json:"error"` + Status string `json:"status"` + Error errorMessage `json:"error"` }{ - Error: errorMessage, + Status: "error", + Error: errorMsg, }) if err != nil { console.Fatalln(probe.NewError(err)) diff --git a/ls.go b/ls.go index f1679088..298a9307 100644 --- a/ls.go +++ b/ls.go @@ -36,6 +36,7 @@ const ( // contentMessage container for content message structure. type contentMessage struct { + Status string `json:"status"` Filetype string `json:"type"` Time time.Time `json:"lastModified"` Size int64 `json:"size"` @@ -57,6 +58,7 @@ func (c contentMessage) String() string { // JSON jsonified content message. func (c contentMessage) JSON() string { + c.Status = "success" jsonMessageBytes, e := json.Marshal(c) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") diff --git a/mirror-main.go b/mirror-main.go index dda4ff40..5b47309c 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -80,6 +80,7 @@ var ( // mirrorMessage container for file mirror messages type mirrorMessage struct { + Status string `json:"status"` Source string `json:"source"` Targets []string `json:"targets"` } @@ -91,6 +92,7 @@ func (m mirrorMessage) String() string { // JSON jsonified mirror message func (m mirrorMessage) JSON() string { + m.Status = "success" mirrorMessageBytes, e := json.Marshal(m) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") diff --git a/rm-main.go b/rm-main.go index eb27a75e..f607205b 100644 --- a/rm-main.go +++ b/rm-main.go @@ -89,7 +89,8 @@ EXAMPLES: // Structured message depending on the type of console. type rmMessage struct { - URL string `json:"url"` + Status string `json:"status"` + URL string `json:"url"` } // Colorized message for console printing. @@ -170,7 +171,7 @@ func rmAll(url string, isRecursive, isIncomplete, isFake bool) { errorIf(err.Trace(entry.URL.String()), "Unable to remove ‘"+entry.URL.String()+"’.") continue } - printMsg(rmMessage{entry.URL.String()}) + printMsg(rmMessage{Status: "success", URL: entry.URL.String()}) } } @@ -200,7 +201,7 @@ func mainRm(ctx *cli.Context) { errorIf(err.Trace(url), "Unable to remove ‘"+url+"’.") continue } - printMsg(rmMessage{url}) + printMsg(rmMessage{Status: "success", URL: url}) } } } diff --git a/session-v3.go b/session-v3.go index d56dd65f..56ea27d1 100644 --- a/session-v3.go +++ b/session-v3.go @@ -85,8 +85,9 @@ type sessionV3Header struct { TotalObjects int `json:"totalObjects"` } -// SessionMessage container for session messages -type SessionMessage struct { +// sessionMessage container for session messages +type sessionMessage struct { + Status string `json:"status"` SessionID string `json:"sessionId"` Time time.Time `json:"time"` CommandType string `json:"commandType"` @@ -122,13 +123,14 @@ func (s sessionV3) String() string { // JSON jsonified session message func (s sessionV3) JSON() string { - sessionMesage := SessionMessage{ + sessionMsg := sessionMessage{ SessionID: s.SessionID, Time: s.Header.When.Local(), CommandType: s.Header.CommandType, CommandArgs: s.Header.CommandArgs, } - sessionBytes, e := json.Marshal(sessionMesage) + sessionMsg.Status = "success" + sessionBytes, e := json.Marshal(sessionMsg) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(sessionBytes) diff --git a/share.go b/share.go index 56b8281e..f421837e 100644 --- a/share.go +++ b/share.go @@ -51,6 +51,7 @@ var ( // Structured share command message. type shareMesssage struct { + Status string `json:"status"` ObjectURL string `json:"url"` ShareURL string `json:"share"` TimeLeft time.Duration `json:"timeLeft"` @@ -74,6 +75,7 @@ func (s shareMesssage) String() string { // JSON - JSONified message for scripting. func (s shareMesssage) JSON() string { + s.Status = "success" shareMessageBytes, e := json.Marshal(s) fatalIf(probe.NewError(e), "Failed to marshal into JSON.") diff --git a/update-main.go b/update-main.go index 09ad6562..b30a0682 100644 --- a/update-main.go +++ b/update-main.go @@ -79,6 +79,7 @@ type mcUpdates struct { // updateMessage container to hold update messages. type updateMessage struct { + Status string `json:"status"` Update bool `json:"update"` Download string `json:"downloadURL"` Version string `json:"version"` @@ -89,7 +90,6 @@ func (u updateMessage) String() string { if !u.Update { return console.Colorize("Update", "You are already running the most recent version of ‘mc’.") } - var msg string if runtime.GOOS == "windows" { msg = "mc.exe cp " + u.Download + " .\\mc.exe" @@ -103,6 +103,7 @@ func (u updateMessage) String() string { // JSON jsonified update message. func (u updateMessage) JSON() string { + u.Status = "success" updateMessageJSONBytes, err := json.Marshal(u) fatalIf(probe.NewError(err), "Unable to marshal into JSON.") diff --git a/version-main.go b/version-main.go index 9cd5d11a..a970d247 100644 --- a/version-main.go +++ b/version-main.go @@ -53,6 +53,7 @@ FLAGS: // Structured message depending on the type of console. type versionMessage struct { + Status string `json:"status"` Version struct { Value string `json:"value"` Format string `json:"format"` @@ -70,6 +71,7 @@ func (v versionMessage) String() string { // JSON'ified message for scripting. func (v versionMessage) JSON() string { + v.Status = "success" msgBytes, e := json.Marshal(v) fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(msgBytes)