Skip to content

Commit ba72833

Browse files
MatteoPologrutoalessio-peruginicmaglie
authored
Get default port address and protocol from sketch profile using monitor -s <sketchPath> (#2329)
--------- Co-authored-by: Alessio Perugini <[email protected]> Co-authored-by: Cristian Maglie <[email protected]>
1 parent d8694ec commit ba72833

File tree

15 files changed

+314
-36
lines changed

15 files changed

+314
-36
lines changed

Diff for: internal/cli/arguments/port.go

+5
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,8 @@ func (p *Port) DetectFQBN(inst *rpc.Instance) (string, *rpc.Port) {
155155
}
156156
return "", nil
157157
}
158+
159+
// IsPortFlagSet returns true if the port address is provided
160+
func (p *Port) IsPortFlagSet() bool {
161+
return p.address != ""
162+
}

Diff for: internal/cli/arguments/sketch.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// InitSketchPath returns an instance of paths.Path pointing to sketchPath.
2626
// If sketchPath is an empty string returns the current working directory.
2727
// In both cases it warns the user if he's using deprecated files
28-
func InitSketchPath(path string) (sketchPath *paths.Path) {
28+
func InitSketchPath(path string, printWarnings bool) (sketchPath *paths.Path) {
2929
if path != "" {
3030
sketchPath = paths.New(path)
3131
} else {
@@ -36,8 +36,10 @@ func InitSketchPath(path string) (sketchPath *paths.Path) {
3636
logrus.Infof("Reading sketch from dir: %s", wd)
3737
sketchPath = wd
3838
}
39-
if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" {
40-
feedback.Warning(msg)
39+
if printWarnings {
40+
if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" {
41+
feedback.Warning(msg)
42+
}
4143
}
4244
return sketchPath
4345
}

Diff for: internal/cli/board/attach.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func initAttachCommand() *cobra.Command {
5353
}
5454

5555
func runAttachCommand(path string, port *arguments.Port, fqbn string) {
56-
sketchPath := arguments.InitSketchPath(path)
56+
sketchPath := arguments.InitSketchPath(path, true)
5757

5858
portAddress, portProtocol, _ := port.GetPortAddressAndProtocol(nil, "", "")
5959
newDefaults, err := sketch.SetSketchDefaults(context.Background(), &rpc.SetSketchDefaultsRequest{

Diff for: internal/cli/compile/compile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
157157
path = args[0]
158158
}
159159

160-
sketchPath := arguments.InitSketchPath(path)
160+
sketchPath := arguments.InitSketchPath(path, true)
161161

162162
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
163163
if err != nil {

Diff for: internal/cli/debug/debug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func runDebugCommand(command *cobra.Command, args []string) {
7474
path = args[0]
7575
}
7676

77-
sketchPath := arguments.InitSketchPath(path)
77+
sketchPath := arguments.InitSketchPath(path, true)
7878
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
7979
if err != nil {
8080
feedback.FatalError(err, feedback.ErrGeneric)

Diff for: internal/cli/monitor/monitor.go

+74-18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"time"
2828

2929
"github.com/arduino/arduino-cli/commands/monitor"
30+
sk "github.com/arduino/arduino-cli/commands/sketch"
3031
"github.com/arduino/arduino-cli/configuration"
3132
"github.com/arduino/arduino-cli/i18n"
3233
"github.com/arduino/arduino-cli/internal/cli/arguments"
@@ -45,13 +46,14 @@ var tr = i18n.Tr
4546
// NewCommand created a new `monitor` command
4647
func NewCommand() *cobra.Command {
4748
var (
48-
raw bool
49-
portArgs arguments.Port
50-
describe bool
51-
configs []string
52-
quiet bool
53-
timestamp bool
54-
fqbn arguments.Fqbn
49+
portArgs arguments.Port
50+
fqbnArg arguments.Fqbn
51+
profileArg arguments.Profile
52+
raw bool
53+
describe bool
54+
configs []string
55+
quiet bool
56+
timestamp bool
5557
)
5658
monitorCommand := &cobra.Command{
5759
Use: "monitor",
@@ -61,38 +63,92 @@ func NewCommand() *cobra.Command {
6163
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
6264
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
6365
Run: func(cmd *cobra.Command, args []string) {
64-
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw)
66+
sketchPath := ""
67+
if len(args) > 0 {
68+
sketchPath = args[0]
69+
}
70+
runMonitorCmd(&portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw)
6571
},
6672
}
6773
portArgs.AddToCommand(monitorCommand)
74+
profileArg.AddToCommand(monitorCommand)
6875
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
6976
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
7077
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
7178
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
7279
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
73-
fqbn.AddToCommand(monitorCommand)
74-
monitorCommand.MarkFlagRequired("port")
80+
fqbnArg.AddToCommand(monitorCommand)
7581
return monitorCommand
7682
}
7783

78-
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool) {
79-
instance := instance.CreateAndInit()
84+
func runMonitorCmd(
85+
portArgs *arguments.Port, fqbnArg *arguments.Fqbn, profileArg *arguments.Profile, sketchPathArg string,
86+
configs []string, describe, timestamp, quiet, raw bool,
87+
) {
8088
logrus.Info("Executing `arduino-cli monitor`")
8189

8290
if !configuration.HasConsole {
8391
quiet = true
8492
}
8593

86-
// TODO: Should use sketch default_port/protocol?
87-
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, "", "")
94+
var (
95+
inst *rpc.Instance
96+
profile *rpc.Profile
97+
fqbn string
98+
defaultPort, defaultProtocol string
99+
)
100+
101+
// Flags takes maximum precedence over sketch.yaml
102+
// If {--port --fqbn --profile} are set we ignore the profile.
103+
// If both {--port --profile} are set we read the fqbn in the following order: profile -> default_fqbn -> discovery
104+
// If only --port is set we read the fqbn in the following order: default_fqbn -> discovery
105+
// If only --fqbn is set we read the port in the following order: default_port
106+
sketchPath := arguments.InitSketchPath(sketchPathArg, false)
107+
sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
108+
if err != nil && !portArgs.IsPortFlagSet() {
109+
feedback.Fatal(
110+
tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", err),
111+
feedback.ErrGeneric,
112+
)
113+
}
114+
if sketch != nil {
115+
defaultPort, defaultProtocol = sketch.GetDefaultPort(), sketch.GetDefaultProtocol()
116+
}
117+
if fqbnArg.String() == "" {
118+
if profileArg.Get() == "" {
119+
inst, profile = instance.CreateAndInitWithProfile(sketch.GetDefaultProfile().GetName(), sketchPath)
120+
} else {
121+
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
122+
}
123+
}
124+
if inst == nil {
125+
inst = instance.CreateAndInit()
126+
}
127+
// Priority on how to retrieve the fqbn
128+
// 1. from flag
129+
// 2. from profile
130+
// 3. from default_fqbn specified in the sketch.yaml
131+
// 4. try to detect from the port
132+
switch {
133+
case fqbnArg.String() != "":
134+
fqbn = fqbnArg.String()
135+
case profile.GetFqbn() != "":
136+
fqbn = profile.GetFqbn()
137+
case sketch.GetDefaultFqbn() != "":
138+
fqbn = sketch.GetDefaultFqbn()
139+
default:
140+
fqbn, _ = portArgs.DetectFQBN(inst)
141+
}
142+
143+
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(inst, defaultPort, defaultProtocol)
88144
if err != nil {
89145
feedback.FatalError(err, feedback.ErrGeneric)
90146
}
91147

92148
enumerateResp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{
93-
Instance: instance,
149+
Instance: inst,
94150
PortProtocol: portProtocol,
95-
Fqbn: fqbn.String(),
151+
Fqbn: fqbn,
96152
})
97153
if err != nil {
98154
feedback.Fatal(tr("Error getting port settings details: %s", err), feedback.ErrGeneric)
@@ -144,9 +200,9 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
144200
}
145201
}
146202
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
147-
Instance: instance,
203+
Instance: inst,
148204
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
149-
Fqbn: fqbn.String(),
205+
Fqbn: fqbn,
150206
PortConfiguration: configuration,
151207
})
152208
if err != nil {

Diff for: internal/cli/upload/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func runUploadCommand(args []string, uploadFieldsArgs map[string]string) {
8989
if len(args) > 0 {
9090
path = args[0]
9191
}
92-
sketchPath := arguments.InitSketchPath(path)
92+
sketchPath := arguments.InitSketchPath(path, true)
9393

9494
if msg := sk.WarnDeprecatedFiles(sketchPath); importDir == "" && importFile == "" && msg != "" {
9595
feedback.Warning(msg)

0 commit comments

Comments
 (0)