diff --git a/pkg/board/remote/adb/adb.go b/pkg/board/remote/adb/adb.go index 6efdb1f4..3ec28b13 100644 --- a/pkg/board/remote/adb/adb.go +++ b/pkg/board/remote/adb/adb.go @@ -120,6 +120,7 @@ func (a *ADBConnection) List(path string) ([]remote.FileInfo, error) { if err := cmd.Start(); err != nil { return nil, err } + defer func() { _ = cmd.Wait() }() r := bufio.NewReader(output) _, err = r.ReadBytes('\n') // Skip the first line @@ -167,6 +168,7 @@ func (a *ADBConnection) Stats(p string) (remote.FileInfo, error) { if err := cmd.Start(); err != nil { return remote.FileInfo{}, err } + defer func() { _ = cmd.Wait() }() r := bufio.NewReader(output) line, err := r.ReadBytes('\n') diff --git a/pkg/board/remote/adb/adb_nowindows.go b/pkg/board/remote/adb/adb_nowindows.go index 32699417..e856b9e2 100644 --- a/pkg/board/remote/adb/adb_nowindows.go +++ b/pkg/board/remote/adb/adb_nowindows.go @@ -18,11 +18,14 @@ package adb import ( + "cmp" "context" "fmt" "io" "github.com/arduino/go-paths-helper" + + "github.com/arduino/arduino-app-cli/pkg/board/remote" ) func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) { @@ -37,7 +40,14 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) { if err := cmd.Start(); err != nil { return nil, err } - return output, nil + return remote.WithCloser{ + Reader: output, + CloseFun: func() error { + err1 := output.Close() + err2 := cmd.Wait() + return cmp.Or(err1, err2) + }, + }, nil } func adbWriteFile(a *ADBConnection, r io.Reader, pathStr string) error { diff --git a/pkg/board/remote/adb/adb_windows.go b/pkg/board/remote/adb/adb_windows.go index 26b752a5..150d9cc0 100644 --- a/pkg/board/remote/adb/adb_windows.go +++ b/pkg/board/remote/adb/adb_windows.go @@ -26,7 +26,7 @@ import ( "github.com/arduino/go-paths-helper" - "github.com/arduino/arduino-app-cli/pkg/board/remote/ssh" + "github.com/arduino/arduino-app-cli/pkg/board/remote" ) func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) { @@ -44,7 +44,7 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) { return nil, err } - return ssh.WithCloser{ + return remote.WithCloser{ Reader: decoded, CloseFun: func() error { err1 := output.Close() diff --git a/pkg/board/remote/remote.go b/pkg/board/remote/remote.go index 9330610a..247e7718 100644 --- a/pkg/board/remote/remote.go +++ b/pkg/board/remote/remote.go @@ -59,3 +59,17 @@ type Cmder interface { Output(ctx context.Context) ([]byte, error) Interactive() (io.WriteCloser, io.Reader, io.Reader, Closer, error) } + +// WithCloser is a helper to create an io.ReadCloser from an io.Reader +// and a close function. +type WithCloser struct { + io.Reader + CloseFun func() error +} + +func (w WithCloser) Close() error { + if w.CloseFun != nil { + return w.CloseFun() + } + return nil +} diff --git a/pkg/board/remote/ssh/ssh.go b/pkg/board/remote/ssh/ssh.go index cca231f5..9b14d8f4 100644 --- a/pkg/board/remote/ssh/ssh.go +++ b/pkg/board/remote/ssh/ssh.go @@ -219,18 +219,6 @@ func (a *SSHConnection) WriteFile(r io.Reader, path string) error { return nil } -type WithCloser struct { - io.Reader - CloseFun func() error -} - -func (w WithCloser) Close() error { - if w.CloseFun != nil { - return w.CloseFun() - } - return nil -} - func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) { session, err := a.client.NewSession() if err != nil { @@ -247,7 +235,7 @@ func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) { return nil, fmt.Errorf("failed to start command: %w", err) } - return WithCloser{ + return remote.WithCloser{ Reader: output, CloseFun: session.Close, }, nil