Skip to content

Commit b4db739

Browse files
committed
improve parsing
1 parent e6c02a7 commit b4db739

File tree

4 files changed

+164
-78
lines changed

4 files changed

+164
-78
lines changed

internal/updater/flasher.go

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
package updater
1717

1818
import (
19-
"bufio"
2019
"context"
2120
"encoding/hex"
2221
"fmt"
23-
"log/slog"
2422
"runtime"
2523
"strconv"
2624
"strings"
@@ -96,18 +94,18 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes
9694
return FlashBoard(ctx, imagePath.String(), version, preserveUser, nil)
9795
}
9896

99-
type TypeEvent int
97+
type EventType int
10098

10199
const (
102-
EventWaiting TypeEvent = 3
103-
EventFlashed TypeEvent = 4
100+
EventLog EventType = iota
101+
EventProgress
104102
)
105103

106104
type FlashEvent struct {
107-
Type TypeEvent
108-
Progress int
109-
MaxProgress int
110-
Log string
105+
Type EventType
106+
Log string
107+
Progress int
108+
Total int
111109
}
112110

113111
type FlahsCallback func(FlashEvent)
@@ -197,25 +195,21 @@ func FlashBoard(ctx context.Context, downloadedImagePath string, version string,
197195
if callback != nil {
198196
progress := 0
199197
w = helper.NewCallbackWriter(func(line string) {
200-
parsedLine, err := parseQdlLogLine(line)
201-
if err != nil {
202-
slog.Warn("could not parse qdl log line", "error", err, "line", line)
203-
return
204-
}
198+
parsedLine := parseQdlLogLine(line)
205199

206200
switch parsedLine.Op {
207-
case Waiting:
201+
case Flashed:
202+
progress++
208203
callback(FlashEvent{
209-
Type: EventWaiting,
210-
Log: line,
204+
Type: EventProgress,
205+
Log: line,
206+
Progress: progress,
207+
Total: totalPartitions,
211208
})
212-
case Flasherd:
213-
progress++
209+
default:
214210
callback(FlashEvent{
215-
Type: EventFlashed,
216-
Log: line,
217-
Progress: progress,
218-
MaxProgress: totalPartitions,
211+
Type: EventLog,
212+
Log: line,
219213
})
220214
}
221215
})
@@ -278,52 +272,3 @@ func checkBoardGPTTable(ctx context.Context, qdlPath, flashDir *paths.Path) erro
278272

279273
return nil
280274
}
281-
282-
type Op int
283-
284-
const (
285-
Waiting Op = 1
286-
Flasherd Op = 2
287-
)
288-
289-
type QDLLogLine struct {
290-
Op Op
291-
Log string
292-
}
293-
294-
func parseQdlLogLine(line string) (QDLLogLine, error) {
295-
line = strings.ToLower(line)
296-
if strings.HasPrefix(line, "waiting for") {
297-
return QDLLogLine{
298-
Op: Waiting,
299-
Log: line,
300-
}, nil
301-
}
302-
303-
if strings.HasPrefix(line, "flashed") {
304-
return QDLLogLine{
305-
Op: Flasherd,
306-
Log: line,
307-
}, nil
308-
}
309-
310-
return QDLLogLine{}, fmt.Errorf("line %q does not match known operations", line)
311-
}
312-
313-
func getTotalPartition(path *paths.Path) (int, error) {
314-
f, err := path.Open()
315-
if err != nil {
316-
return 0, err
317-
}
318-
319-
r := bufio.NewScanner(f)
320-
var total int
321-
for r.Scan() {
322-
c := strings.Count(r.Text(), "<program")
323-
total += c
324-
}
325-
if err := r.Err(); err != nil {
326-
return 0, err
327-
}
328-
return total, nil
329-
}

internal/updater/parseqdl.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package updater
2+
3+
import (
4+
"bufio"
5+
"strings"
6+
7+
"github.com/arduino/go-paths-helper"
8+
)
9+
10+
type Op int
11+
12+
const (
13+
Waiting Op = iota
14+
Flashed
15+
Unknown
16+
)
17+
18+
type QDLLogLine struct {
19+
Op Op
20+
Log string
21+
}
22+
23+
func parseQdlLogLine(line string) QDLLogLine {
24+
lower := strings.ToLower(line)
25+
26+
switch {
27+
case strings.HasPrefix(lower, "waiting for"):
28+
return QDLLogLine{
29+
Op: Waiting,
30+
Log: line,
31+
}
32+
case strings.HasPrefix(lower, "flashed"):
33+
return QDLLogLine{
34+
Op: Flashed,
35+
Log: line,
36+
}
37+
default:
38+
return QDLLogLine{
39+
Op: Unknown,
40+
Log: line,
41+
}
42+
}
43+
}
44+
45+
func getTotalPartition(path *paths.Path) (int, error) {
46+
f, err := path.Open()
47+
if err != nil {
48+
return 0, err
49+
}
50+
51+
r := bufio.NewScanner(f)
52+
var total int
53+
for r.Scan() {
54+
c := strings.Count(r.Text(), "<program")
55+
total += c
56+
}
57+
if err := r.Err(); err != nil {
58+
return 0, err
59+
}
60+
return total, nil
61+
}

internal/updater/parseqdl_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package updater
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestParseQdlLogLine(t *testing.T) {
10+
tests := []struct {
11+
line string
12+
want QDLLogLine
13+
wantErr bool
14+
}{
15+
{
16+
line: "Waiting for EDL device...",
17+
want: QDLLogLine{
18+
Op: Waiting,
19+
Log: "Waiting for EDL device...",
20+
},
21+
},
22+
{
23+
line: "waiting for programmer...",
24+
want: QDLLogLine{
25+
Op: Waiting,
26+
Log: "waiting for programmer...",
27+
},
28+
},
29+
{
30+
line: `flashed "xbl_a" successfully`,
31+
want: QDLLogLine{
32+
Op: Flashed,
33+
Log: `flashed "xbl_a" successfully`,
34+
},
35+
},
36+
{
37+
line: `flashed "rootfs" successfully at 65058kB/s`,
38+
want: QDLLogLine{
39+
Op: Flashed,
40+
Log: `flashed "rootfs" successfully at 65058kB/s`,
41+
},
42+
},
43+
{
44+
45+
line: "13 patches applied",
46+
want: QDLLogLine{
47+
Op: Unknown,
48+
Log: "13 patches applied",
49+
},
50+
},
51+
{
52+
line: "partition 0 is now bootable",
53+
want: QDLLogLine{
54+
Op: Unknown,
55+
Log: "partition 0 is now bootable",
56+
},
57+
},
58+
}
59+
60+
for _, tt := range tests {
61+
t.Run(tt.line, func(t *testing.T) {
62+
result, err := parseQdlLogLine(tt.line)
63+
if tt.wantErr {
64+
require.Error(t, err)
65+
} else {
66+
require.NoError(t, err)
67+
require.Equal(t, tt.want, result)
68+
}
69+
70+
})
71+
}
72+
}

service/service_flash.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ func (s *flasherServerImpl) Flash(req *flasher.FlashRequest, stream flasher.Flas
129129

130130
flashCB(&flasher.TaskProgress{Name: "flash", Message: "Starting"})
131131
if err := updater.FlashBoard(ctx, imagePath.String(), rel.Version, req.GetPreserveUser(), func(fe updater.FlashEvent) {
132-
flashCB(&flasher.TaskProgress{
133-
Name: "flash",
134-
Message: fe.Log,
135-
Progress: int64(fe.Progress),
136-
Total: int64(fe.MaxProgress),
137-
})
132+
switch fe.Type {
133+
case updater.EventLog:
134+
flashCB(&flasher.TaskProgress{
135+
Name: "flash",
136+
Message: fe.Log,
137+
})
138+
case updater.EventProgress:
139+
flashCB(&flasher.TaskProgress{
140+
Name: "flash",
141+
Message: fe.Log,
142+
Progress: int64(fe.Progress),
143+
Total: int64(fe.Total),
144+
})
145+
}
138146

139147
}); err != nil {
140148
return err

0 commit comments

Comments
 (0)