Skip to content

Commit 8df8033

Browse files
committed
Factored --discovery-timeout flag and added timeout to board autodetection
1 parent 0679228 commit 8df8033

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

cli/arguments/discovery_timeout.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package arguments
17+
18+
import (
19+
"time"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// DiscoveryTimeout is the timeout given to discoveries to detect ports.
25+
type DiscoveryTimeout struct {
26+
timeout time.Duration
27+
}
28+
29+
// AddToCommand adds the flags used to set fqbn to the specified Command
30+
func (d *DiscoveryTimeout) AddToCommand(cmd *cobra.Command) {
31+
cmd.Flags().DurationVar(&d.timeout, "discovery-timeout", time.Second, tr("Max time to wait for port discovery, e.g.: 30s, 1m"))
32+
}
33+
34+
// Get returns the timeout
35+
func (d *DiscoveryTimeout) Get() time.Duration {
36+
return d.timeout
37+
}

cli/arguments/port.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
type Port struct {
4141
address string
4242
protocol string
43-
timeout time.Duration
43+
timeout DiscoveryTimeout
4444
}
4545

4646
// AddToCommand adds the flags used to set port and protocol to the specified Command
@@ -53,7 +53,7 @@ func (p *Port) AddToCommand(cmd *cobra.Command) {
5353
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
5454
return GetInstalledProtocols(), cobra.ShellCompDirectiveDefault
5555
})
56-
cmd.Flags().DurationVar(&p.timeout, "discovery-timeout", 5*time.Second, tr("Max time to wait for port discovery, e.g.: 30s, 1m"))
56+
p.timeout.AddToCommand(cmd)
5757
}
5858

5959
// GetPortAddressAndProtocol returns only the port address and the port protocol
@@ -127,7 +127,7 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
127127
}
128128
}()
129129

130-
deadline := time.After(p.timeout)
130+
deadline := time.After(p.timeout.Get())
131131
for {
132132
select {
133133
case portEvent := <-eventChan:
@@ -154,14 +154,17 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
154154

155155
// GetSearchTimeout returns the timeout
156156
func (p *Port) GetSearchTimeout() time.Duration {
157-
return p.timeout
157+
return p.timeout.Get()
158158
}
159159

160160
// DetectFQBN tries to identify the board connected to the port and returns the
161161
// discovered Port object together with the FQBN. If the port does not match
162162
// exactly 1 board,
163163
func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
164-
detectedPorts, err := board.List(&rpc.BoardListRequest{Instance: inst})
164+
detectedPorts, err := board.List(&rpc.BoardListRequest{
165+
Instance: inst,
166+
Timeout: p.timeout.Get().Milliseconds(),
167+
})
165168
if err != nil {
166169
feedback.Errorf(tr("Error during FQBN detection: %v", err))
167170
os.Exit(errorcodes.ErrGeneric)

cli/board/list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
"fmt"
2020
"os"
2121
"sort"
22-
"time"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores"
24+
"github.com/arduino/arduino-cli/cli/arguments"
2525
"github.com/arduino/arduino-cli/cli/errorcodes"
2626
"github.com/arduino/arduino-cli/cli/feedback"
2727
"github.com/arduino/arduino-cli/cli/instance"
@@ -33,21 +33,21 @@ import (
3333
)
3434

3535
var (
36-
timeout time.Duration
37-
watch bool
36+
timeoutArg arguments.DiscoveryTimeout
37+
watch bool
3838
)
3939

4040
func initListCommand() *cobra.Command {
4141
listCommand := &cobra.Command{
4242
Use: "list",
4343
Short: tr("List connected boards."),
4444
Long: tr("Detects and displays a list of boards connected to the current computer."),
45-
Example: " " + os.Args[0] + " board list --timeout 10s",
45+
Example: " " + os.Args[0] + " board list --discovery-timeout 10s",
4646
Args: cobra.NoArgs,
4747
Run: runListCommand,
4848
}
4949

50-
listCommand.Flags().DurationVar(&timeout, "discovery-timeout", time.Second, tr("Max time to wait for port discovery, e.g.: 30s, 1m"))
50+
timeoutArg.AddToCommand(listCommand)
5151
listCommand.Flags().BoolVarP(&watch, "watch", "w", false, tr("Command keeps running and prints list of connected boards whenever there is a change."))
5252

5353
return listCommand
@@ -66,7 +66,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
6666

6767
ports, err := board.List(&rpc.BoardListRequest{
6868
Instance: inst,
69-
Timeout: timeout.Milliseconds(),
69+
Timeout: timeoutArg.Get().Milliseconds(),
7070
})
7171
if err != nil {
7272
feedback.Errorf(tr("Error detecting boards: %v"), err)

0 commit comments

Comments
 (0)