Skip to content

Commit 0ca5cf8

Browse files
committed
refactoring
1 parent a23f45b commit 0ca5cf8

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

pkg/flasherapi/flasherapi.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package flasherapi
1717

1818
import (
19+
"bufio"
1920
"context"
21+
"io"
2022
"log/slog"
2123
"strings"
2224

@@ -27,34 +29,42 @@ const R0_IMAGE_VERSION_ID = "20250807-136"
2729

2830
// GetOSImageVersion returns the version of the OS image used in the board.
2931
// It is used by the AppLab to enforce image version compatibility.
30-
func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) (string, error) {
32+
func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) string {
3133

32-
output, err := conn.GetCmd("cat", "/etc/buildinfo").Output(ctx)
34+
f, err := conn.ReadFile("/etc/buildinfo")
3335
if err != nil {
34-
return R0_IMAGE_VERSION_ID, err
36+
slog.Warn("Unable to read buildinfo file", "err", err, "using_default", R0_IMAGE_VERSION_ID)
37+
return R0_IMAGE_VERSION_ID
3538
}
39+
defer f.Close()
3640

37-
if version, ok := ParseOSImageVersion(string(output)); ok {
38-
slog.Info("find OS Image version", "version", version)
39-
return version, nil
41+
if version, ok := parseOSImageVersion(f); ok {
42+
slog.Info("found OS Image version", "version", version)
43+
return version
4044
}
41-
slog.Info("Unable to find OS Image version", "using default version", R0_IMAGE_VERSION_ID)
42-
return R0_IMAGE_VERSION_ID, nil
45+
slog.Warn("Unable to find OS Image version", "using_default", R0_IMAGE_VERSION_ID)
46+
return R0_IMAGE_VERSION_ID
4347
}
4448

45-
func ParseOSImageVersion(buildInfo string) (string, bool) {
46-
for _, line := range strings.Split(buildInfo, "\n") {
47-
line = strings.TrimSpace(line)
49+
func parseOSImageVersion(r io.Reader) (string, bool) {
50+
scanner := bufio.NewScanner(r)
51+
for scanner.Scan() {
52+
line := strings.TrimSpace(scanner.Text())
4853

4954
key, value, ok := strings.Cut(line, "=")
5055
if !ok || key != "BUILD_ID" {
5156
continue
5257
}
5358

54-
version := strings.Trim(value, "\"' ")
59+
version := strings.Trim(value, `"' `)
5560
if version != "" {
5661
return version, true
5762
}
5863
}
64+
65+
if err := scanner.Err(); err != nil {
66+
return "", false
67+
}
68+
5969
return "", false
6070
}

pkg/flasherapi/flasherapi_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
package flasherapi
1717

18-
import "testing"
18+
import (
19+
"strings"
20+
"testing"
21+
)
1922

2023
func TestParseOSImageVersion(t *testing.T) {
2124
tests := []struct {
@@ -44,7 +47,7 @@ func TestParseOSImageVersion(t *testing.T) {
4447

4548
for _, tt := range tests {
4649
t.Run(tt.name, func(t *testing.T) {
47-
got, ok := ParseOSImageVersion(tt.input)
50+
got, ok := parseOSImageVersion(strings.NewReader(tt.input))
4851
if ok != tt.found || got != tt.expected {
4952
t.Fatalf("got (%q, %v), expected (%q, %v)", got, ok, tt.expected, tt.found)
5053
}

0 commit comments

Comments
 (0)