Skip to content

Commit 15bcdd7

Browse files
authored
feat(pkg/board): add custom name property in network mode (#537)
1 parent 83e52a5 commit 15bcdd7

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

cmd/arduino-app-cli/board/board.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,31 @@ func NewBoardCmd() *cobra.Command {
2727
Use: "board",
2828
Short: "Manage boards",
2929
Long: "",
30-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
30+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
3131
if host != "" {
3232
conn, err := adb.FromHost(host, "")
3333
if err != nil {
3434
panic(fmt.Errorf("failed to connect to ADB host %s: %w", host, err))
3535
}
3636
cmd.SetContext(context.WithValue(cmd.Context(), remoteConnKey, conn))
37-
return
37+
return nil
3838
}
3939

4040
boards, err := board.FromFQBN(cmd.Context(), fqbn)
4141
if err != nil {
42-
panic(err)
42+
return fmt.Errorf("failed to get boards for FQBN %s: %w", fqbn, err)
4343
}
4444
if len(boards) == 0 {
45-
panic(fmt.Errorf("no boards found for FQBN %s", fqbn))
45+
return fmt.Errorf("no boards found for FQBN %s", fqbn)
4646
}
4747
conn, err := boards[0].GetConnection()
4848
if err != nil {
49-
panic(fmt.Errorf("failed to connect to board: %w", err))
49+
return fmt.Errorf("failed to connect to board %s: %w", boards[0].BoardName, err)
5050
}
5151

5252
cmd.SetContext(context.WithValue(cmd.Context(), remoteConnKey, conn))
5353
cmd.SetContext(context.WithValue(cmd.Context(), boardsListKey, boards))
54+
return nil
5455
},
5556
}
5657
fsCmd.PersistentFlags().StringVarP(&fqbn, "fqbn", "b", "arduino:zephyr:unoq", "fqbn of the board")

pkg/board/board.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,37 @@ func FromFQBN(ctx context.Context, fqbn string) ([]Board, error) {
8383
switch port.GetPort().GetProtocol() {
8484
case SerialProtocol:
8585
serial := strings.ToLower(port.GetPort().GetHardwareId()) // in windows this is uppercase.
86+
87+
// TODO: we should store the board custom name in the product id so we can get it from the discovery service.
88+
var customName string
89+
if conn, err := adb.FromSerial(serial, ""); err == nil {
90+
if name, err := GetCustomName(ctx, conn); err == nil {
91+
customName = name
92+
}
93+
}
94+
8695
boards = append(boards, Board{
87-
Protocol: SerialProtocol,
88-
Serial: serial,
89-
BoardName: boardName,
96+
Protocol: SerialProtocol,
97+
Serial: serial,
98+
BoardName: boardName,
99+
CustomName: customName,
90100
})
91101
case NetworkProtocol:
102+
var customName string
103+
if name, ok := port.GetPort().GetProperties()["hostname"]; ok {
104+
// take the part before the first dot as custom name
105+
idx := strings.Index(name, ".")
106+
if idx == -1 {
107+
idx = len(name)
108+
}
109+
customName = name[:idx]
110+
}
111+
92112
boards = append(boards, Board{
93-
Protocol: NetworkProtocol,
94-
Address: port.GetPort().GetAddress(),
95-
BoardName: boardName,
113+
Protocol: NetworkProtocol,
114+
Address: port.GetPort().GetAddress(),
115+
BoardName: boardName,
116+
CustomName: customName,
96117
})
97118
default:
98119
slog.Warn("unknown protocol", "protocol", port.GetPort().GetProtocol())
@@ -108,22 +129,6 @@ func FromFQBN(ctx context.Context, fqbn string) ([]Board, error) {
108129
}
109130
})
110131

111-
// Get board names
112-
for i := range boards {
113-
switch boards[i].Protocol {
114-
case SerialProtocol:
115-
// TODO: we should store the board custom name in the product id so we can get it from the discovery service.
116-
var name string
117-
if conn, err := adb.FromSerial(boards[i].Serial, ""); err == nil {
118-
if name, err = GetCustomName(ctx, conn); err == nil {
119-
boards[i].CustomName = name
120-
}
121-
}
122-
case NetworkProtocol:
123-
// TODO: get from mDNS
124-
}
125-
}
126-
127132
return boards, nil
128133
}
129134

0 commit comments

Comments
 (0)