@@ -18,7 +18,7 @@ import (
1818 "github.com/arduino/go-paths-helper"
1919
2020 "github.com/bcmi-labs/orchestrator/pkg/board/remote"
21- "github.com/bcmi-labs/orchestrator/pkg/board/remote/common "
21+ "github.com/bcmi-labs/orchestrator/pkg/x/ports "
2222)
2323
2424const username = "arduino"
@@ -56,27 +56,27 @@ func FromHost(host string, adbPath string) (*ADBConnection, error) {
5656 return FromSerial (host , adbPath )
5757}
5858
59- func (a * ADBConnection ) Forward (ctx context.Context , remotePort int ) (int , error ) {
60- hostAvailablePort , err := common .GetAvailablePort ()
61- if err != nil {
62- return 0 , fmt .Errorf ("failed to find an available port: %w" , err )
59+ func (a * ADBConnection ) Forward (ctx context.Context , localPort int , remotePort int ) error {
60+ if ! ports .IsAvailable (localPort ) {
61+ return remote .ErrPortAvailable
6362 }
6463
65- local := fmt .Sprintf ("tcp:%d" , hostAvailablePort )
64+ local := fmt .Sprintf ("tcp:%d" , localPort )
6665 remote := fmt .Sprintf ("tcp:%d" , remotePort )
6766 cmd , err := paths .NewProcess (nil , a .adbPath , "-s" , a .host , "forward" , local , remote )
6867 if err != nil {
69- return hostAvailablePort , err
68+ return err
7069 }
7170 if err := cmd .RunWithinContext (ctx ); err != nil {
72- return hostAvailablePort , fmt .Errorf (
71+ return fmt .Errorf (
7372 "failed to forward ADB port %s to %s: %w" ,
7473 local ,
7574 remote ,
7675 err ,
7776 )
7877 }
79- return hostAvailablePort , nil
78+
79+ return nil
8080}
8181
8282func (a * ADBConnection ) ForwardKillAll (ctx context.Context ) error {
0 commit comments