Skip to content

Commit ed2abde

Browse files
committed
Resolve conflicts with mereg issue_41_flasher_api
2 parents e8c0369 + 8e59c43 commit ed2abde

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

pkg/flasherapi/flasherapi.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,45 @@ package flasherapi
1717

1818
import (
1919
"context"
20+
"log/slog"
21+
"strings"
2022

2123
"github.com/arduino/arduino-app-cli/pkg/board/remote"
2224
)
2325

2426
// GetOSImageVersion returns the version of the OS image used in the board.
2527
// It is used by the AppLab to enforce image version compatibility.
26-
func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) string {
27-
// if no version is set, return a default value
28-
return "20251123-159"
28+
func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) (string, error) {
29+
const defaultVersion = "20250807-136"
30+
31+
output, err := conn.GetCmd("cat /etc/buildinfo").Output(ctx)
32+
if err != nil {
33+
return defaultVersion, err
34+
}
35+
36+
if version, ok := ParseOSImageVersion(string(output)); ok {
37+
slog.Info("find OS Image version", "version", version)
38+
return version, nil
39+
}
40+
slog.Info("Unable to find OS Image version", "using default version", defaultVersion)
41+
return defaultVersion, nil
42+
}
43+
44+
func ParseOSImageVersion(buildInfo string) (string, bool) {
45+
for _, line := range strings.Split(buildInfo, "\n") {
46+
line = strings.TrimSpace(line)
47+
48+
key, value, ok := strings.Cut(line, "=")
49+
if !ok || key != "BUILD_ID" {
50+
continue
51+
}
52+
53+
version := strings.Trim(value, "\"' ")
54+
if version != "" {
55+
return version, true
56+
}
57+
}
58+
return "", false
2959
}
3060

3161
type OSImageRelease struct {
@@ -50,9 +80,12 @@ const R0_IMAGE_VERSION_ID = "20250807-136"
5080
// according to the current and target OS image versions.
5181
//
5282
// Preservation is supported if both versions are not the R0 image.
53-
func IsUserPartitionPreservationSupported(ctx context.Context, conn remote.RemoteConn, targetImageVersion OSImageRelease) bool {
54-
currentImageVersion := GetOSImageVersion(ctx, conn)
55-
return !(targetImageVersion.ID == R0_IMAGE_VERSION_ID || currentImageVersion == R0_IMAGE_VERSION_ID)
83+
func IsUserPartitionPreservationSupported(ctx context.Context, conn remote.RemoteConn, targetImageVersion OSImageRelease) (bool, error) {
84+
currentImageVersion, err := GetOSImageVersion(ctx, conn)
85+
if err != nil {
86+
return false, err
87+
}
88+
return !(targetImageVersion.ID == R0_IMAGE_VERSION_ID || currentImageVersion == R0_IMAGE_VERSION_ID), err
5689
}
5790

5891
type FlashStep string

pkg/flasherapi/flasherapi_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package flasherapi
2+
3+
import "testing"
4+
5+
func TestParseOSImageVersion(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
input string
9+
expected string
10+
found bool
11+
}{
12+
{
13+
name: "valid build id",
14+
input: "BUILD_ID=\"20251006-395\"\nVARIANT_ID=xfce",
15+
expected: "20251006-395",
16+
found: true,
17+
},
18+
{
19+
name: "missing build id",
20+
input: "VARIANT_ID=xfce\n",
21+
found: false,
22+
},
23+
{
24+
name: "empty build id",
25+
input: "BUILD_ID=\n",
26+
found: false,
27+
},
28+
}
29+
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
got, ok := ParseOSImageVersion(tt.input)
33+
if ok != tt.found || got != tt.expected {
34+
t.Fatalf("got (%q, %v), expected (%q, %v)", got, ok, tt.expected, tt.found)
35+
}
36+
})
37+
}
38+
}

0 commit comments

Comments
 (0)