Skip to content

Commit 24aa177

Browse files
Return lines with error and print the previous 10 and next 10 lines
1 parent cb49f7b commit 24aa177

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

cmd/state-mcp/internal/registry/tools.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ func DownloadLogsTool() Tool {
9696
}
9797

9898
runner := downloadlogs.New(p)
99-
params := downloadlogs.NewParams(url)
99+
params := downloadlogs.NewParams()
100+
params.LogUrl = url
100101
err = runner.Run(params)
101102
if err != nil {
102103
return mcp.NewToolResultError(fmt.Sprintf("error downloading logs: %s", errs.JoinMessage(err))), nil

internal/runners/mcp/downloadlogs/downloadlogs.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"strings"
910

1011
"github.com/ActiveState/cli/internal/output"
1112
"github.com/ActiveState/cli/internal/primer"
@@ -22,13 +23,11 @@ func New(p *primer.Values) *DownloadLogsRunner {
2223
}
2324

2425
type Params struct {
25-
logUrl string
26+
LogUrl string
2627
}
2728

28-
func NewParams(logUrl string) *Params {
29-
return &Params{
30-
logUrl: logUrl,
31-
}
29+
func NewParams() *Params {
30+
return &Params{}
3231
}
3332

3433
// Example: {"body": {"facility": "INFO", "msg": "..."}, "artifact_id": "...", "timestamp": "2025-08-12T19:23:51.702971", "type": "artifact_progress", "source": "build-wrapper", "pid": 19}
@@ -39,44 +38,58 @@ type LogLine struct {
3938
}
4039

4140
func (runner *DownloadLogsRunner) Run(params *Params) error {
42-
response, err := http.Get(params.logUrl)
41+
response, err := http.Get(params.LogUrl)
4342
if err != nil {
4443
return fmt.Errorf("error while downloading logs: %v", err)
4544
}
4645
defer response.Body.Close()
4746

48-
if response.StatusCode != 200 {
47+
if response.StatusCode != http.StatusOK {
4948
body, _ := io.ReadAll(response.Body)
5049
return fmt.Errorf("error fetching logs: status %d, %s", response.StatusCode, body)
5150
}
5251

5352
scanner := bufio.NewScanner(response.Body)
5453

55-
startPrinting := false
56-
54+
// Read all lines, parse and only store the text messages
55+
var lines []string
5756
for scanner.Scan() {
58-
var logLine LogLine
5957
line := scanner.Text()
60-
58+
var logLine LogLine
6159
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-
}
7160
continue
7261
}
73-
74-
runner.output.Print(msg + "\n")
62+
lines = append(lines, logLine.Body.Msg)
7563
}
76-
7764
if err := scanner.Err(); err != nil {
7865
return fmt.Errorf("error reading log content: %v", err)
7966
}
8067

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+
8194
return nil
8295
}

0 commit comments

Comments
 (0)