-
Notifications
You must be signed in to change notification settings - Fork 7
feat(pkg/boards): get on board image version #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c876a0
initial flasher import
mirkoCrobu 8e59c43
implement getOSImageVersion
mirkoCrobu a23f45b
refactoring
mirkoCrobu 0ca5cf8
refactoring
mirkoCrobu 661be98
code review fix
mirkoCrobu 016a622
refactoring test
mirkoCrobu b6ea56f
remove unused parameter
mirkoCrobu 0b31374
enhance tests
mirkoCrobu 5454fa7
move os_image funcs into new file
mirkoCrobu 57f388c
remove log
mirkoCrobu c9951cb
code review fix
mirkoCrobu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package flasherapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "strings" | ||
|
|
||
| "github.com/arduino/arduino-app-cli/pkg/board/remote" | ||
| ) | ||
|
|
||
| // GetOSImageVersion returns the version of the OS image used in the board. | ||
| // It is used by the AppLab to enforce image version compatibility. | ||
| func GetOSImageVersion(ctx context.Context, conn remote.RemoteConn) (string, error) { | ||
| const defaultVersion = "20250807-136" | ||
|
|
||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| output, err := conn.GetCmd("cat /etc/buildinfo").Output(ctx) | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return defaultVersion, err | ||
| } | ||
|
|
||
| if version, ok := ParseOSImageVersion(string(output)); ok { | ||
| slog.Info("find OS Image version", "version", version) | ||
| return version, nil | ||
| } | ||
| slog.Info("Unable to find OS Image version", "using default version", defaultVersion) | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return defaultVersion, nil | ||
| } | ||
|
|
||
| func ParseOSImageVersion(buildInfo string) (string, bool) { | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for _, line := range strings.Split(buildInfo, "\n") { | ||
| line = strings.TrimSpace(line) | ||
|
|
||
| key, value, ok := strings.Cut(line, "=") | ||
| if !ok || key != "BUILD_ID" { | ||
| continue | ||
| } | ||
|
|
||
| version := strings.Trim(value, "\"' ") | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if version != "" { | ||
| return version, true | ||
| } | ||
| } | ||
| return "", false | ||
| } | ||
|
|
||
| type OSImageRelease struct { | ||
| VersionLabel string | ||
| ID string | ||
| Latest bool | ||
| } | ||
|
|
||
| func ListAvailableOSImages() []OSImageRelease { | ||
| return []OSImageRelease{ | ||
| { | ||
| ID: "20251123-159", | ||
| VersionLabel: "r159 (2025-11-23)", | ||
| Latest: true, | ||
| }, | ||
| } | ||
| } | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| func IsUserPartitionPreservationSupported(conn remote.RemoteConn, targetImageVersion OSImageRelease) bool { | ||
| // targetImageVersion is the version of the image to be flashed | ||
| // some older versions do not support user partition preservation | ||
| // so this has to be considered here | ||
| return true | ||
| } | ||
|
|
||
| type FlashStep string | ||
|
|
||
| const ( | ||
| FlashStepPrecheck FlashStep = "precheck" | ||
| FlashStepDetection FlashStep = "detection" | ||
| FlashStepDownloading FlashStep = "downloading" | ||
| FlashStepExtracting FlashStep = "extracting" | ||
| FlashStepFlashing FlashStep = "flashing" | ||
| ) | ||
|
|
||
| type FlashEvent struct { | ||
| Step FlashStep | ||
| OverallProgress int // percentage 0-100 | ||
| Message string // log message to show on the UI | ||
| } | ||
|
|
||
| func Flash( | ||
| ctx context.Context, // context to cancel to interrupt the flashing process | ||
| // conn remote.RemoteConn, // is this required for the flash process? | ||
| imageVersion OSImageRelease, // OS image version to flash | ||
| preserveUserPartition bool, // whether to preserve the user partition or not | ||
| eventCB func(event FlashEvent), // Callback, sends progress events to the caller | ||
| ) error { | ||
| // The disk space check is done before starting the flashing process. | ||
| return InsufficientDiskSpaceError{ | ||
| RequiredSpaceMB: 2048, | ||
| AvailableSpaceMB: 1024, | ||
| } | ||
| } | ||
|
|
||
| // Errors returned by the Flash function above. | ||
| // Assertions can be done on the caller side to show better error messages. | ||
|
|
||
| type QDLFlashError struct { | ||
| Details string | ||
| Cause error | ||
| } | ||
|
|
||
| type DownloadError struct { | ||
| Details string | ||
| } | ||
|
|
||
| type InsufficientDiskSpaceError struct { | ||
| RequiredSpaceMB int64 | ||
| AvailableSpaceMB int64 | ||
| } | ||
|
|
||
| func (e InsufficientDiskSpaceError) Error() string { | ||
| return "insufficient disk space" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package flasherapi | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestParseOSImageVersion(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| found bool | ||
| }{ | ||
| { | ||
| name: "valid build id", | ||
| input: "BUILD_ID=\"20251006-395\"\nVARIANT_ID=xfce", | ||
mirkoCrobu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expected: "20251006-395", | ||
| found: true, | ||
| }, | ||
| { | ||
| name: "missing build id", | ||
| input: "VARIANT_ID=xfce\n", | ||
| found: false, | ||
| }, | ||
| { | ||
| name: "empty build id", | ||
| input: "BUILD_ID=\n", | ||
| found: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, ok := ParseOSImageVersion(tt.input) | ||
| if ok != tt.found || got != tt.expected { | ||
| t.Fatalf("got (%q, %v), expected (%q, %v)", got, ok, tt.expected, tt.found) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.