From 9a33f35e114c426936c9a67a795fe16e896a06b7 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Wed, 20 Sep 2023 11:20:06 +0200 Subject: [PATCH] Get default port address and protocol from sketch profile --- internal/cli/monitor/monitor.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/internal/cli/monitor/monitor.go b/internal/cli/monitor/monitor.go index 65b5c599a76..319f6a459f4 100644 --- a/internal/cli/monitor/monitor.go +++ b/internal/cli/monitor/monitor.go @@ -26,6 +26,7 @@ import ( "strings" "github.com/arduino/arduino-cli/commands/monitor" + "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/configuration" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/internal/cli/arguments" @@ -44,12 +45,13 @@ var tr = i18n.Tr // NewCommand created a new `monitor` command func NewCommand() *cobra.Command { var ( - raw bool - portArgs arguments.Port - describe bool - configs []string - quiet bool - fqbn arguments.Fqbn + raw bool + portArgs arguments.Port + describe bool + configs []string + quiet bool + fqbn arguments.Fqbn + sketchPath string ) monitorCommand := &cobra.Command{ Use: "monitor", @@ -59,7 +61,7 @@ func NewCommand() *cobra.Command { " " + os.Args[0] + " monitor -p /dev/ttyACM0\n" + " " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe", Run: func(cmd *cobra.Command, args []string) { - runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw) + runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw, sketchPath) }, } portArgs.AddToCommand(monitorCommand) @@ -67,12 +69,12 @@ func NewCommand() *cobra.Command { monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port.")) monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is =[,=]...")) monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output.")) + monitorCommand.Flags().StringVarP(&sketchPath, "sketch", "s", "", tr("Path to the sketch")) fqbn.AddToCommand(monitorCommand) - monitorCommand.MarkFlagRequired("port") return monitorCommand } -func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool) { +func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool, sketchPath string) { instance := instance.CreateAndInit() logrus.Info("Executing `arduino-cli monitor`") @@ -80,8 +82,17 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str quiet = true } - // TODO: Should use sketch default_port/protocol? - portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, "", "") + addressDefault := "" + protocolDefault := "" + if sketchPath != "" { + sketch, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath}) + if err != nil { + feedback.FatalError(err, feedback.ErrGeneric) + } + addressDefault = sketch.GetDefaultPort() + protocolDefault = sketch.GetDefaultProtocol() + } + portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, addressDefault, protocolDefault) if err != nil { feedback.FatalError(err, feedback.ErrGeneric) }