Skip to content

Commit 75116fc

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

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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)