Skip to content

Commit badc853

Browse files
MatteoPologrutoalessio-perugini
authored andcommitted
Get default port address and protocol from sketch profile
1 parent 4b2a32b commit badc853

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

internal/cli/monitor/monitor.go

+23-12
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+
"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+
raw bool
50+
portArgs arguments.Port
51+
describe bool
52+
configs []string
53+
quiet bool
54+
timestamp bool
55+
fqbn arguments.Fqbn
56+
sketchPath string
5557
)
5658
monitorCommand := &cobra.Command{
5759
Use: "monitor",
@@ -61,7 +63,7 @@ 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+
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw, sketchPath)
6567
},
6668
}
6769
portArgs.AddToCommand(monitorCommand)
@@ -70,21 +72,30 @@ func NewCommand() *cobra.Command {
7072
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
7173
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
7274
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
75+
monitorCommand.Flags().StringVarP(&sketchPath, "sketch", "s", "", tr("Path to the sketch"))
7376
fqbn.AddToCommand(monitorCommand)
74-
monitorCommand.MarkFlagRequired("port")
7577
return monitorCommand
7678
}
7779

78-
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool) {
80+
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool, sketchPath string) {
7981
instance := instance.CreateAndInit()
8082
logrus.Info("Executing `arduino-cli monitor`")
8183

8284
if !configuration.HasConsole {
8385
quiet = true
8486
}
8587

86-
// TODO: Should use sketch default_port/protocol?
87-
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, "", "")
88+
addressDefault := ""
89+
protocolDefault := ""
90+
if sketchPath != "" {
91+
sketch, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath})
92+
if err != nil {
93+
feedback.FatalError(err, feedback.ErrGeneric)
94+
}
95+
addressDefault = sketch.GetDefaultPort()
96+
protocolDefault = sketch.GetDefaultProtocol()
97+
}
98+
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, addressDefault, protocolDefault)
8899
if err != nil {
89100
feedback.FatalError(err, feedback.ErrGeneric)
90101
}

0 commit comments

Comments
 (0)