-
Notifications
You must be signed in to change notification settings - Fork 9
feat: forward brick container ports #296
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
Open
lucarin91
wants to merge
6
commits into
arduino:main
Choose a base branch
from
lucarin91:forward-brick-container-ports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca24ca8
feat: forward brick container ports
lucarin91 a2514ad
fixup! feat: forward brick container ports
lucarin91 bde542e
fixup! fixup! feat: forward brick container ports
lucarin91 be51851
fix tests
lucarin91 fd19bc8
remove hardcoded webview in app.yaml
lucarin91 1ffa99f
Merge remote-tracking branch 'origin/main' into forward-brick-contain…
lucarin91 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -16,14 +16,15 @@ | |
| package bricksindex | ||
|
|
||
| import ( | ||
| "io" | ||
| "iter" | ||
| "maps" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/arduino/go-paths-helper" | ||
| yaml "github.com/goccy/go-yaml" | ||
|
|
||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/peripherals" | ||
| "github.com/arduino/arduino-app-cli/internal/store" | ||
| ) | ||
|
|
||
| type BricksIndex struct { | ||
|
|
@@ -62,6 +63,7 @@ type Brick struct { | |
| RequireModel bool `yaml:"require_model"` | ||
| Variables []BrickVariable `yaml:"variables,omitempty"` | ||
| Ports []string `yaml:"ports,omitempty"` | ||
| containerPorts []string `yaml:"-"` | ||
| ModelName string `yaml:"model_name,omitempty"` | ||
| MountDevicesIntoContainer bool `yaml:"mount_devices_into_container,omitempty"` | ||
| RequiredDevices []peripherals.DeviceClass `yaml:"required_devices,omitempty"` | ||
|
|
@@ -87,19 +89,67 @@ func (b Brick) GetDefaultVariables() iter.Seq2[string, string] { | |
| } | ||
| } | ||
|
|
||
| func unmarshalBricksIndex(content io.Reader) (*BricksIndex, error) { | ||
| var index BricksIndex | ||
| if err := yaml.NewDecoder(content).Decode(&index); err != nil { | ||
| return nil, err | ||
| func (b Brick) GetPorts() []string { | ||
| portsSet := make(map[string]struct{}, len(b.Ports)+len(b.containerPorts)) | ||
| for _, p := range b.Ports { | ||
| portsSet[p] = struct{}{} | ||
| } | ||
| return &index, nil | ||
| for _, p := range b.containerPorts { | ||
| portsSet[p] = struct{}{} | ||
| } | ||
| return slices.Collect(maps.Keys(portsSet)) | ||
| } | ||
|
|
||
| func Load(dir *paths.Path) (*BricksIndex, error) { | ||
| content, err := dir.Join("bricks-list.yaml").Open() | ||
| func Load(staticstore *store.StaticStore) (*BricksIndex, error) { | ||
| content, err := staticstore.GetAssetsFolder().Join("bricks-list.yaml").Open() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer content.Close() | ||
| return unmarshalBricksIndex(content) | ||
|
|
||
| var index BricksIndex | ||
| if err := yaml.NewDecoder(content).Decode(&index); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Parse the compose file to read the extra ports. | ||
| for i := range index.Bricks { | ||
| composeFilePath, err := staticstore.GetBrickComposeFilePathFromID(index.Bricks[i].ID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if !index.Bricks[i].RequireContainer || composeFilePath.NotExist() { | ||
| continue | ||
| } | ||
|
|
||
| f, err := composeFilePath.Open() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var compose struct { | ||
| Services map[string]struct { | ||
| Ports []string `yaml:"ports,omitempty"` | ||
| } `yaml:"services"` | ||
| } | ||
| if err := yaml.NewDecoder(f).Decode(&compose); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, service := range compose.Services { | ||
| for _, portStr := range service.Ports { | ||
| if strings.Contains(portStr, ":") { | ||
| parts := strings.Split(portStr, ":") | ||
| hostPort := parts[len(parts)-2] // Extract the host port (the one before the last colon) | ||
| index.Bricks[i].containerPorts = append(index.Bricks[i].containerPorts, hostPort) | ||
| } else { | ||
| index.Bricks[i].containerPorts = append(index.Bricks[i].containerPorts, portStr) | ||
| } | ||
| } | ||
|
Comment on lines
+140
to
+147
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a unit test for the port parsing |
||
| } | ||
| } | ||
|
|
||
| return &index, nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having a defer f.Close() inside the loop causes many pending f.Close() at the same time, since the defer will be called at the end of Load().
We could extract a helper function to get the ports: getComposePorts(composeFilePath *paths.Path) ([]string, error)