11package downloadlogs
22
33import (
4+ "bufio"
5+ "encoding/json"
46 "fmt"
57 "io"
68 "net/http"
9+ "strings"
710
811 "github.com/ActiveState/cli/internal/output"
912 "github.com/ActiveState/cli/internal/primer"
@@ -20,29 +23,73 @@ func New(p *primer.Values) *DownloadLogsRunner {
2023}
2124
2225type Params struct {
23- logUrl string
26+ LogUrl string
2427}
2528
26- func NewParams (logUrl string ) * Params {
27- return & Params {
28- logUrl : logUrl ,
29- }
29+ func NewParams () * Params {
30+ return & Params {}
31+ }
32+
33+ // Example: {"body": {"facility": "INFO", "msg": "..."}, "artifact_id": "...", "timestamp": "2025-08-12T19:23:51.702971", "type": "artifact_progress", "source": "build-wrapper", "pid": 19}
34+ type LogLine struct {
35+ Body struct {
36+ Msg string `json:"msg"`
37+ } `json:"body"`
3038}
3139
3240func (runner * DownloadLogsRunner ) Run (params * Params ) error {
33- response , err := http .Get (params .logUrl )
41+ response , err := http .Get (params .LogUrl )
3442 if err != nil {
3543 return fmt .Errorf ("error while downloading logs: %v" , err )
3644 }
3745 defer response .Body .Close ()
3846
39- body , err := io .ReadAll (response .Body )
40- if err != nil {
41- return fmt .Errorf ("error reading response body: %v" , err )
42- }
43- if response .StatusCode != 200 {
47+ if response .StatusCode != http .StatusOK {
48+ body , _ := io .ReadAll (response .Body )
4449 return fmt .Errorf ("error fetching logs: status %d, %s" , response .StatusCode , body )
4550 }
46- runner .output .Print (string (body ))
51+
52+ scanner := bufio .NewScanner (response .Body )
53+
54+ // Read all lines, parse and only store the text messages
55+ var lines []string
56+ for scanner .Scan () {
57+ line := scanner .Text ()
58+ var logLine LogLine
59+ if err := json .Unmarshal ([]byte (line ), & logLine ); err != nil {
60+ continue
61+ }
62+ lines = append (lines , logLine .Body .Msg )
63+ }
64+ if err := scanner .Err (); err != nil {
65+ return fmt .Errorf ("error reading log content: %v" , err )
66+ }
67+
68+ // Check what lines contain the keyword "error" and print the previous 10 and next 10 lines
69+ printedLines := make (map [int ]bool )
70+ for i , line := range lines {
71+ if strings .Contains (strings .ToLower (line ), "error" ) {
72+ start := i - 10
73+ if start < 0 {
74+ start = 0
75+ }
76+ end := i + 10
77+ if end >= len (lines ) {
78+ end = len (lines ) - 1
79+ }
80+
81+ for j := start ; j <= end ; j ++ {
82+ if ! printedLines [j ] {
83+ // Print ellipsis if there are skipped lines
84+ if j > 0 && ! printedLines [j - 1 ] {
85+ runner .output .Print ("[...]" )
86+ }
87+ runner .output .Print (lines [j ])
88+ printedLines [j ] = true
89+ }
90+ }
91+ }
92+ }
93+
4794 return nil
4895}
0 commit comments