Skip to content

Commit 8e59c43

Browse files
committed
implement getOSImageVersion
1 parent 0c876a0 commit 8e59c43

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

pkg/flasherapi/flasherapi.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@ package flasherapi
22

33
import (
44
"context"
5+
"log/slog"
6+
"strings"
57

68
"github.com/arduino/arduino-app-cli/pkg/board/remote"
79
)
810

911
// GetOSImageVersion returns the version of the OS image used in the board.
1012
// It is used by the AppLab to enforce image version compatibility.
11-
func GetOSImageVersion(conn remote.RemoteConn) string {
12-
// if no version is set, return a default value
13-
return "20251123-159"
13+
func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) (string, error) {
14+
const defaultVersion = "20250807-136"
15+
16+
output, err := conn.GetCmd("cat /etc/buildinfo").Output(ctx)
17+
if err != nil {
18+
return defaultVersion, err
19+
}
20+
21+
if version, ok := ParseOSImageVersion(string(output)); ok {
22+
slog.Info("find OS Image version", "version", version)
23+
return version, nil
24+
}
25+
slog.Info("Unable to find OS Image version", "using default version", defaultVersion)
26+
return defaultVersion, nil
27+
}
28+
29+
func ParseOSImageVersion(buildInfo string) (string, bool) {
30+
for _, line := range strings.Split(buildInfo, "\n") {
31+
line = strings.TrimSpace(line)
32+
33+
key, value, ok := strings.Cut(line, "=")
34+
if !ok || key != "BUILD_ID" {
35+
continue
36+
}
37+
38+
version := strings.Trim(value, "\"' ")
39+
if version != "" {
40+
return version, true
41+
}
42+
}
43+
return "", false
1444
}
1545

1646
type OSImageRelease struct {

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)