diff --git a/pkg/cmd/listen.go b/pkg/cmd/listen.go index c368d3fb1..d96d99155 100644 --- a/pkg/cmd/listen.go +++ b/pkg/cmd/listen.go @@ -122,7 +122,8 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error { return err } - if !lc.printJSON && !lc.onlyPrintSecret && !lc.skipUpdate { + isJSONMode := strings.ToUpper(lc.format) == outputFormatJSON || lc.printJSON + if !lc.onlyPrintSecret && !lc.skipUpdate && !isJSONMode { version.CheckLatestVersion() } @@ -229,35 +230,30 @@ func withSIGTERMCancel(ctx context.Context, onCancel func()) context.Context { func (lc *listenCmd) createVisitor(logger *log.Logger, format string, printJSON bool) *websocket.Visitor { var s *spinner.Spinner + isJSONMode := strings.ToUpper(format) == outputFormatJSON || printJSON return &websocket.Visitor{ VisitError: func(ee websocket.ErrorElement) error { ansi.StopSpinner(s, "", logger.Out) switch ee.Error.(type) { case proxy.FailedToPostError: - color := ansi.Color(os.Stdout) - localTime := time.Now().Format(timeLayout) - - errStr := fmt.Sprintf("%s [%s] Failed to POST: %v\n", - color.Faint(localTime), - color.Red("ERROR"), - ee.Error, - ) - fmt.Println(errStr) + if !isJSONMode { + color := ansi.Color(os.Stdout) + localTime := time.Now().Format(timeLayout) + errStr := fmt.Sprintf("%s [%s] Failed to POST: %v\n", + color.Faint(localTime), + color.Red("ERROR"), + ee.Error, + ) + fmt.Fprint(os.Stderr, errStr) + } // Don't exit program return nil case proxy.FailedToReadResponseError: - color := ansi.Color(os.Stdout) - localTime := time.Now().Format(timeLayout) - - errStr := fmt.Sprintf("%s [%s] Failed to read response from endpoint, error = %v\n", - color.Faint(localTime), - color.Red("ERROR"), - ee.Error, - ) - log.Errorf("%s", errStr) - + if !isJSONMode { + log.Errorf("Failed to read response from endpoint: %v", ee.Error) + } // Don't exit program return nil default: @@ -266,6 +262,9 @@ func (lc *listenCmd) createVisitor(logger *log.Logger, format string, printJSON } }, VisitStatus: func(se websocket.StateElement) error { + if isJSONMode { + return nil + } switch se.State { case websocket.Loading: s = ansi.StartNewSpinner("Getting ready...", logger.Out) diff --git a/pkg/cmd/logs/tail.go b/pkg/cmd/logs/tail.go index aaffc65c0..5ac78b091 100644 --- a/pkg/cmd/logs/tail.go +++ b/pkg/cmd/logs/tail.go @@ -178,7 +178,10 @@ func (tailCmd *TailCmd) runTailCmd(cmd *cobra.Command, args []string) error { return err } - version.CheckLatestVersion() + isJSONMode := strings.ToUpper(tailCmd.format) == outputFormatJSON + if !isJSONMode { + version.CheckLatestVersion() + } logger := log.StandardLogger() @@ -263,6 +266,7 @@ func (tailCmd *TailCmd) convertArgs() error { func createVisitor(logger *log.Logger, format string) *websocket.Visitor { var s *spinner.Spinner + isJSONMode := strings.ToUpper(format) == outputFormatJSON return &websocket.Visitor{ VisitError: func(ee websocket.ErrorElement) error { @@ -270,11 +274,18 @@ func createVisitor(logger *log.Logger, format string) *websocket.Visitor { return ee.Error }, VisitWarning: func(we websocket.WarningElement) error { - color := ansi.Color(os.Stdout) - fmt.Printf("%s %s\n", color.Yellow("Warning"), we.Warning) + if isJSONMode { + fmt.Fprintf(os.Stderr, "Warning: %s\n", we.Warning) + } else { + color := ansi.Color(os.Stdout) + fmt.Printf("%s %s\n", color.Yellow("Warning"), we.Warning) + } return nil }, VisitStatus: func(se websocket.StateElement) error { + if isJSONMode { + return nil + } switch se.State { case websocket.Loading: s = ansi.StartNewSpinner("Getting ready...", logger.Out) @@ -295,8 +306,8 @@ func createVisitor(logger *log.Logger, format string) *websocket.Visitor { sanitizePayload(&log) - if strings.ToUpper(format) == outputFormatJSON { - fmt.Println(ansi.ColorizeJSON(de.Marshaled, false, os.Stdout)) + if isJSONMode { + fmt.Println(de.Marshaled) return nil } diff --git a/pkg/parsers/parsers.go b/pkg/parsers/parsers.go index f08831721..7829b6255 100644 --- a/pkg/parsers/parsers.go +++ b/pkg/parsers/parsers.go @@ -78,7 +78,7 @@ func ParsePath(httpPath string, queryRespMap map[string]gjson.Result) (string, e newPath = append(newPath, value) } - if len(pathParts)%2 == 0 { + if pathParts[len(pathParts)-1] != "" { newPath = append(newPath, pathParts[len(pathParts)-1]) } diff --git a/pkg/proxy/endpoint.go b/pkg/proxy/endpoint.go index 858f7ac59..54b26c9fb 100644 --- a/pkg/proxy/endpoint.go +++ b/pkg/proxy/endpoint.go @@ -155,7 +155,7 @@ func (c *EndpointClient) PostV2(evtCtx eventContext) error { } } - resp, err := http.DefaultClient.Do(req) + resp, err := c.cfg.HTTPClient.Do(req) if err != nil { c.cfg.OutCh <- websocket.ErrorElement{ Error: FailedToPostError{Err: err},