From 60a29bae96054bfc2841e7633a2a46b4cd643e23 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 19 Aug 2015 02:27:40 -0700 Subject: [PATCH] Remove json indent for easing parsing Indentation while it is nice, leads to issues when parsing where delimiters on readline() will fail since we are sending many self contained json's in a streaming fashion, having a proper readline() based delimiter is helpful in terms of parsing. --- error.go | 13 +++++++++---- pkg/console/console.go | 9 --------- pkg/console/themes.go | 3 --- print-structs.go | 24 ++++++++++++------------ version-main.go | 10 ++++++---- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/error.go b/error.go index e99de18d..eeb7b02a 100644 --- a/error.go +++ b/error.go @@ -31,7 +31,6 @@ func fatalIf(err *probe.Error) { } if globalJSONFlag { errorMessage := struct { - // Error string `json:"error"` Cause error `json:"cause"` Type string `json:"type"` CallTrace []probe.TracePoint `json:"trace,omitempty"` @@ -44,7 +43,10 @@ func fatalIf(err *probe.Error) { if globalDebugFlag { errorMessage.CallTrace = err.CallTrace } - json, _ := json.Marshal(errorMessage) + json, err := json.Marshal(errorMessage) + if err != nil { + panic(err) + } console.Println(string(json)) os.Exit(1) } @@ -61,7 +63,6 @@ func errorIf(err *probe.Error) { } if globalJSONFlag { errorMessage := struct { - // Error string `json:"error"` Cause error `json:"cause"` Type string `json:"type"` CallTrace []probe.TracePoint `json:"trace,omitempty"` @@ -74,12 +75,16 @@ func errorIf(err *probe.Error) { if globalDebugFlag { errorMessage.CallTrace = err.CallTrace } - json, _ := json.Marshal(errorMessage) + json, err := json.Marshal(errorMessage) + if err != nil { + panic(err) + } console.Println(string(json)) return } if !globalDebugFlag { console.Errorln(err.ToGoError()) + return } console.Errorln(err) } diff --git a/pkg/console/console.go b/pkg/console/console.go index b8dc6135..37a088c6 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -53,7 +53,6 @@ type Theme struct { Dir *color.Color Command *color.Color SessionID *color.Color - JSON *color.Color Bar *color.Color PrintC *color.Color Print *color.Color @@ -258,14 +257,6 @@ var ( } return fmt.Sprintf(format, data...) } - - // JSON helper to print json strings - JSON = func(format string, data ...interface{}) string { - if isatty.IsTerminal(os.Stdout.Fd()) { - return themesDB[currThemeName].JSON.SprintfFunc()(format, data...) - } - return fmt.Sprintf(format, data...) - } ) var ( diff --git a/pkg/console/themes.go b/pkg/console/themes.go index 4754f5cb..4c873a40 100644 --- a/pkg/console/themes.go +++ b/pkg/console/themes.go @@ -30,7 +30,6 @@ var MiniTheme = Theme{ SessionID: (color.New(color.FgYellow, color.Bold)), Size: (color.New(color.FgYellow)), Time: (color.New(color.FgGreen)), - JSON: (color.New(color.FgWhite, color.Italic)), Bar: (color.New(color.FgGreen, color.Bold)), PrintC: (color.New(color.FgGreen, color.Bold)), Print: (color.New()), @@ -48,7 +47,6 @@ var WhiteTheme = Theme{ SessionID: (color.New(color.FgWhite, color.Bold)), Size: (color.New(color.FgWhite, color.Bold)), Time: (color.New(color.FgWhite, color.Bold)), - JSON: (color.New(color.FgWhite, color.Bold, color.Italic)), Bar: (color.New(color.FgWhite, color.Bold)), PrintC: (color.New(color.FgWhite, color.Bold)), Print: (color.New()), @@ -66,7 +64,6 @@ var NoColorTheme = Theme{ SessionID: new(color.Color), Size: new(color.Color), Time: new(color.Color), - JSON: new(color.Color), Bar: new(color.Color), PrintC: new(color.Color), Print: new(color.Color), diff --git a/print-structs.go b/print-structs.go index 00932dfd..0aed9ac3 100644 --- a/print-structs.go +++ b/print-structs.go @@ -52,11 +52,11 @@ func (s diffV1) String() string { diffMessage.FirstURL = s.firstURL diffMessage.SecondURL = s.secondURL diffMessage.Diff = s.diffType - diffJSONBytes, err := json.MarshalIndent(diffMessage, "", "\t") + diffJSONBytes, err := json.Marshal(diffMessage) if err != nil { panic(err) } - return console.JSON(string(diffJSONBytes) + "\n") + return string(diffJSONBytes) + "\n" } // SessionJSONMessage json container for session messages @@ -80,11 +80,11 @@ func (s sessionV2) String() string { CommandType: s.Header.CommandType, CommandArgs: s.Header.CommandArgs, } - sessionJSONBytes, err := json.MarshalIndent(sessionMesage, "", "\t") + sessionJSONBytes, err := json.Marshal(sessionMesage) if err != nil { panic(err) } - return console.JSON(string(sessionJSONBytes) + "\n") + return string(sessionJSONBytes) + "\n" } // ContentMessage container for content message structure @@ -108,11 +108,11 @@ func (c ContentMessage) String() string { }() return message + "\n" } - jsonMessageBytes, err := json.MarshalIndent(c, "", "\t") + jsonMessageBytes, err := json.Marshal(c) if err != nil { panic(err) } - return console.JSON(string(jsonMessageBytes) + "\n") + return string(jsonMessageBytes) + "\n" } // CopyMessage container for file copy messages @@ -127,11 +127,11 @@ func (c CopyMessage) String() string { if !globalJSONFlag { return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) } - copyMessageBytes, err := json.MarshalIndent(c, "", "\t") + copyMessageBytes, err := json.Marshal(c) if err != nil { panic(err) } - return console.JSON(string(copyMessageBytes) + "\n") + return string(copyMessageBytes) + "\n" } // MirrorMessage container for file mirror messages @@ -146,11 +146,11 @@ func (s MirrorMessage) String() string { if !globalJSONFlag { return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets) } - mirrorMessageBytes, err := json.MarshalIndent(s, "", "\t") + mirrorMessageBytes, err := json.Marshal(s) if err != nil { panic(err) } - return console.JSON(string(mirrorMessageBytes) + "\n") + return string(mirrorMessageBytes) + "\n" } // ShareMessage container for share messages @@ -164,12 +164,12 @@ func (s ShareMessage) String() string { if !globalJSONFlag { return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) } - shareMessageBytes, err := json.MarshalIndent(s, "", "\t") + shareMessageBytes, err := json.Marshal(s) if err != nil { panic(err) } // json encoding escapes ampersand into its unicode character which is not usable directly for share // and fails with cloud storage. convert them back so that they are usable shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) - return console.JSON("%s\n", string(shareMessageBytes)) + return fmt.Sprintf("%s\n", string(shareMessageBytes)) } diff --git a/version-main.go b/version-main.go index 39864ae7..351d17a3 100644 --- a/version-main.go +++ b/version-main.go @@ -58,13 +58,15 @@ func mainVersion(ctxx *cli.Context) { Format string `json:"format"` } if globalJSONFlag { - tB, err := json.MarshalIndent(struct { - Version Version `json:"version"` - }{Version: Version{t, "RFC3339Nano"}}, "", "\t") + tB, err := json.Marshal( + struct { + Version Version `json:"version"` + }{Version: Version{t, "RFC3339Nano"}}, + ) if err != nil { console.Fatalln(probe.NewError(err)) } - console.Println(console.JSON(string(tB))) + console.Println(string(tB)) return } console.Println(t.Format(http.TimeFormat))