Skip to content

Commit

Permalink
Get default port address and protocol from sketch profile
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoPologruto committed Sep 20, 2023
1 parent 28fc9d6 commit 9a33f35
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -59,29 +61,38 @@ 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)
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
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 <ID>=<value>[,<ID>=<value>]..."))
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`")

if !configuration.HasConsole {
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)
}
Expand Down

0 comments on commit 9a33f35

Please sign in to comment.