Skip to content

Commit 710cee4

Browse files
Get default port address and protocol from sketch profile
1 parent 28fc9d6 commit 710cee4

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

internal/cli/monitor/monitor.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
"github.com/arduino/arduino-cli/commands/monitor"
29+
"github.com/arduino/arduino-cli/commands/sketch"
2930
"github.com/arduino/arduino-cli/configuration"
3031
"github.com/arduino/arduino-cli/i18n"
3132
"github.com/arduino/arduino-cli/internal/cli/arguments"
@@ -44,12 +45,13 @@ var tr = i18n.Tr
4445
// NewCommand created a new `monitor` command
4546
func NewCommand() *cobra.Command {
4647
var (
47-
raw bool
48-
portArgs arguments.Port
49-
describe bool
50-
configs []string
51-
quiet bool
52-
fqbn arguments.Fqbn
48+
raw bool
49+
portArgs arguments.Port
50+
describe bool
51+
configs []string
52+
quiet bool
53+
fqbn arguments.Fqbn
54+
sketchPath string
5355
)
5456
monitorCommand := &cobra.Command{
5557
Use: "monitor",
@@ -59,29 +61,38 @@ func NewCommand() *cobra.Command {
5961
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
6062
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
6163
Run: func(cmd *cobra.Command, args []string) {
62-
runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw)
64+
runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw, sketchPath)
6365
},
6466
}
6567
portArgs.AddToCommand(monitorCommand)
6668
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
6769
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
6870
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
6971
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
72+
monitorCommand.Flags().StringVarP(&sketchPath, "sketch", "s", "", tr("Path to the sketch"))
7073
fqbn.AddToCommand(monitorCommand)
71-
monitorCommand.MarkFlagRequired("port")
7274
return monitorCommand
7375
}
7476

75-
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool) {
77+
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool, sketchPath string) {
7678
instance := instance.CreateAndInit()
7779
logrus.Info("Executing `arduino-cli monitor`")
7880

7981
if !configuration.HasConsole {
8082
quiet = true
8183
}
8284

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

0 commit comments

Comments
 (0)