|
1 | 1 | package downloadlogs |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
4 | 6 | "fmt" |
5 | 7 | "io" |
6 | 8 | "net/http" |
@@ -29,20 +31,52 @@ func NewParams(logUrl string) *Params { |
29 | 31 | } |
30 | 32 | } |
31 | 33 |
|
| 34 | +// Example: {"body": {"facility": "INFO", "msg": "..."}, "artifact_id": "...", "timestamp": "2025-08-12T19:23:51.702971", "type": "artifact_progress", "source": "build-wrapper", "pid": 19} |
| 35 | +type LogLine struct { |
| 36 | + Body struct { |
| 37 | + Msg string `json:"msg"` |
| 38 | + } `json:"body"` |
| 39 | +} |
| 40 | + |
32 | 41 | func (runner *DownloadLogsRunner) Run(params *Params) error { |
33 | 42 | response, err := http.Get(params.logUrl) |
34 | 43 | if err != nil { |
35 | 44 | return fmt.Errorf("error while downloading logs: %v", err) |
36 | 45 | } |
37 | 46 | defer response.Body.Close() |
38 | 47 |
|
39 | | - body, err := io.ReadAll(response.Body) |
40 | | - if err != nil { |
41 | | - return fmt.Errorf("error reading response body: %v", err) |
42 | | - } |
43 | 48 | if response.StatusCode != 200 { |
| 49 | + body, _ := io.ReadAll(response.Body) |
44 | 50 | return fmt.Errorf("error fetching logs: status %d, %s", response.StatusCode, body) |
45 | 51 | } |
46 | | - runner.output.Print(string(body)) |
| 52 | + |
| 53 | + scanner := bufio.NewScanner(response.Body) |
| 54 | + |
| 55 | + startPrinting := false |
| 56 | + |
| 57 | + for scanner.Scan() { |
| 58 | + var logLine LogLine |
| 59 | + line := scanner.Text() |
| 60 | + |
| 61 | + if err := json.Unmarshal([]byte(line), &logLine); err != nil { |
| 62 | + continue // Skip malformed lines |
| 63 | + } |
| 64 | + |
| 65 | + msg := logLine.Body.Msg |
| 66 | + |
| 67 | + if !startPrinting { |
| 68 | + if msg == "Dependencies downloaded and unpacked." { |
| 69 | + startPrinting = true |
| 70 | + } |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + runner.output.Print(msg + "\n") |
| 75 | + } |
| 76 | + |
| 77 | + if err := scanner.Err(); err != nil { |
| 78 | + return fmt.Errorf("error reading log content: %v", err) |
| 79 | + } |
| 80 | + |
47 | 81 | return nil |
48 | 82 | } |
0 commit comments