From 8769d3eddf5bf1000d5d06c5c1cc6ebbdb265f35 Mon Sep 17 00:00:00 2001 From: mohit-nagaraj Date: Thu, 17 Oct 2024 19:24:32 +0530 Subject: [PATCH] output formatting --- commands/apps.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/commands/apps.go b/commands/apps.go index 5db806fb3..4c36b8cdb 100644 --- a/commands/apps.go +++ b/commands/apps.go @@ -14,6 +14,7 @@ limitations under the License. package commands import ( + "bufio" "bytes" "encoding/json" "fmt" @@ -610,6 +611,11 @@ func RunAppsGetLogs(c *CmdConfig) error { return err } + outputJSON, err := c.Doit.GetBool(c.NS, "output-json") + if err != nil { + return err + } + logs, err := c.Apps().GetLogs(appID, deploymentID, component, logType, logFollow, logTail) if err != nil { return err @@ -631,6 +637,18 @@ func RunAppsGetLogs(c *CmdConfig) error { } r := strings.NewReader(data.Data) + if outputJSON { + content, err := io.ReadAll(r) + if err != nil { + return nil, err + } + logParts := strings.SplitN(string(content), " ", 3) + if len(logParts) > 2 { + jsonLog := logParts[2] + return strings.NewReader(jsonLog), nil + } + } + return r, nil } @@ -654,7 +672,20 @@ func RunAppsGetLogs(c *CmdConfig) error { } defer resp.Body.Close() - io.Copy(c.Out, resp.Body) + scanner := bufio.NewScanner(resp.Body) + for scanner.Scan() { + logLine := scanner.Text() + if outputJSON { + logParts := strings.SplitN(logLine, " ", 3) + if len(logParts) > 2 { + logLine = logParts[2] + } + } + fmt.Fprintln(c.Out, logLine) + } + if err := scanner.Err(); err != nil { + return err + } } else { warn("No logs found for app component") }