Skip to content

Commit

Permalink
output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-nagaraj committed Oct 17, 2024
1 parent 7f7a3c0 commit 8769d3e
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion commands/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package commands

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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")
}
Expand Down

0 comments on commit 8769d3e

Please sign in to comment.