Skip to content

Commit 2f3be29

Browse files
committed
user a better way to handler wait on close
1 parent 503fa9c commit 2f3be29

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

pkg/board/remote/adb/adb_nowindows.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
package adb
1919

2020
import (
21+
"cmp"
2122
"context"
2223
"fmt"
2324
"io"
2425

2526
"github.com/arduino/go-paths-helper"
27+
28+
"github.com/arduino/arduino-app-cli/pkg/board/remote"
2629
)
2730

2831
func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
@@ -37,12 +40,14 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
3740
if err := cmd.Start(); err != nil {
3841
return nil, err
3942
}
40-
defer func() {
41-
go func() {
42-
_ = cmd.Wait()
43-
}()
44-
}()
45-
return output, nil
43+
return remote.WithCloser{
44+
Reader: output,
45+
CloseFun: func() error {
46+
err1 := output.Close()
47+
err2 := cmd.Wait()
48+
return cmp.Or(err1, err2)
49+
},
50+
}, nil
4651
}
4752

4853
func adbWriteFile(a *ADBConnection, r io.Reader, pathStr string) error {

pkg/board/remote/adb/adb_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"github.com/arduino/go-paths-helper"
2828

29-
"github.com/arduino/arduino-app-cli/pkg/board/remote/ssh"
29+
"github.com/arduino/arduino-app-cli/pkg/board/remote"
3030
)
3131

3232
func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
@@ -44,7 +44,7 @@ func adbReadFile(a *ADBConnection, path string) (io.ReadCloser, error) {
4444
return nil, err
4545
}
4646

47-
return ssh.WithCloser{
47+
return remote.WithCloser{
4848
Reader: decoded,
4949
CloseFun: func() error {
5050
err1 := output.Close()

pkg/board/remote/remote.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,17 @@ type Cmder interface {
5959
Output(ctx context.Context) ([]byte, error)
6060
Interactive() (io.WriteCloser, io.Reader, io.Reader, Closer, error)
6161
}
62+
63+
// WithCloser is a helper to create an io.ReadCloser from an io.Reader
64+
// and a close function.
65+
type WithCloser struct {
66+
io.Reader
67+
CloseFun func() error
68+
}
69+
70+
func (w WithCloser) Close() error {
71+
if w.CloseFun != nil {
72+
return w.CloseFun()
73+
}
74+
return nil
75+
}

pkg/board/remote/ssh/ssh.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ func (a *SSHConnection) WriteFile(r io.Reader, path string) error {
219219
return nil
220220
}
221221

222-
type WithCloser struct {
223-
io.Reader
224-
CloseFun func() error
225-
}
226-
227-
func (w WithCloser) Close() error {
228-
if w.CloseFun != nil {
229-
return w.CloseFun()
230-
}
231-
return nil
232-
}
233-
234222
func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) {
235223
session, err := a.client.NewSession()
236224
if err != nil {
@@ -247,7 +235,7 @@ func (a *SSHConnection) ReadFile(path string) (io.ReadCloser, error) {
247235
return nil, fmt.Errorf("failed to start command: %w", err)
248236
}
249237

250-
return WithCloser{
238+
return remote.WithCloser{
251239
Reader: output,
252240
CloseFun: session.Close,
253241
}, nil

0 commit comments

Comments
 (0)