Skip to content

Commit 289f6d5

Browse files
committed
discovery: Do not start process on Discovery creation
Added a Start method for that and now Close will also kill the running process.
1 parent 7662cfa commit 289f6d5

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

arduino/discovery/discovery.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"os/exec"
2425

2526
properties "github.com/arduino/go-properties-orderedmap"
2627

@@ -32,6 +33,7 @@ type Discovery struct {
3233
in io.WriteCloser
3334
out io.ReadCloser
3435
outJSON *json.Decoder
36+
cmd *exec.Cmd
3537
}
3638

3739
// BoardPort is a generic port descriptor
@@ -56,21 +58,27 @@ func NewFromCommandLine(args ...string) (*Discovery, error) {
5658
return nil, fmt.Errorf("creating discovery process: %s", err)
5759
}
5860
disc := &Discovery{}
59-
if in, err := cmd.StdinPipe(); err == nil {
60-
disc.in = in
61+
disc.cmd = cmd
62+
return disc, nil
63+
}
64+
65+
// Start starts the specified discovery
66+
func (d *Discovery) Start() error {
67+
if in, err := d.cmd.StdinPipe(); err == nil {
68+
d.in = in
6169
} else {
62-
return nil, fmt.Errorf("creating stdin pipe for discovery: %s", err)
70+
return fmt.Errorf("creating stdin pipe for discovery: %s", err)
6371
}
64-
if out, err := cmd.StdoutPipe(); err == nil {
65-
disc.out = out
66-
disc.outJSON = json.NewDecoder(disc.out)
72+
if out, err := d.cmd.StdoutPipe(); err == nil {
73+
d.out = out
74+
d.outJSON = json.NewDecoder(d.out)
6775
} else {
68-
return nil, fmt.Errorf("creating stdout pipe for discovery: %s", err)
76+
return fmt.Errorf("creating stdout pipe for discovery: %s", err)
6977
}
70-
if err := cmd.Start(); err != nil {
71-
return nil, fmt.Errorf("starting discovery: %s", err)
78+
if err := d.cmd.Start(); err != nil {
79+
return fmt.Errorf("starting discovery process: %s", err)
7280
}
73-
return disc, nil
81+
return nil
7482
}
7583

7684
// List retrieve the port list from this discovery
@@ -87,11 +95,15 @@ func (d *Discovery) List() ([]*BoardPort, error) {
8795

8896
// Close stops the Discovery and free the resources
8997
func (d *Discovery) Close() error {
98+
// TODO: Send QUIT for safe close or terminate process after a small timeout
9099
if err := d.in.Close(); err != nil {
91100
return fmt.Errorf("closing stdin pipe: %s", err)
92101
}
93102
if err := d.out.Close(); err != nil {
94103
return fmt.Errorf("closing stdout pipe: %s", err)
95104
}
105+
if d.cmd != nil {
106+
d.cmd.Process.Kill()
107+
}
96108
return nil
97109
}

0 commit comments

Comments
 (0)