Skip to content

Commit f45c9fc

Browse files
remove sketch flag
1 parent 41c4c56 commit f45c9fc

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

internal/cli/monitor/monitor.go

+54-24
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ import (
2727
"time"
2828

2929
"github.com/arduino/arduino-cli/commands/monitor"
30-
"github.com/arduino/arduino-cli/commands/sketch"
30+
sk "github.com/arduino/arduino-cli/commands/sketch"
3131
"github.com/arduino/arduino-cli/configuration"
3232
"github.com/arduino/arduino-cli/i18n"
3333
"github.com/arduino/arduino-cli/internal/cli/arguments"
3434
"github.com/arduino/arduino-cli/internal/cli/feedback"
3535
"github.com/arduino/arduino-cli/internal/cli/instance"
3636
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3737
"github.com/arduino/arduino-cli/table"
38+
"github.com/arduino/go-paths-helper"
3839
"github.com/fatih/color"
3940
"github.com/sirupsen/logrus"
4041
"github.com/spf13/cobra"
@@ -46,14 +47,14 @@ var tr = i18n.Tr
4647
// NewCommand created a new `monitor` command
4748
func NewCommand() *cobra.Command {
4849
var (
49-
raw bool
5050
portArgs arguments.Port
51+
fqbnArg arguments.Fqbn
52+
profileArg arguments.Profile
53+
raw bool
5154
describe bool
5255
configs []string
5356
quiet bool
5457
timestamp bool
55-
fqbn arguments.Fqbn
56-
sketchPath string
5758
)
5859
monitorCommand := &cobra.Command{
5960
Use: "monitor",
@@ -63,47 +64,76 @@ func NewCommand() *cobra.Command {
6364
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
6465
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
6566
Run: func(cmd *cobra.Command, args []string) {
66-
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw, sketchPath)
67+
sketchPath := ""
68+
if len(args) > 0 {
69+
sketchPath = args[0]
70+
}
71+
var portProvidedFromFlag bool
72+
if p := cmd.Flags().Lookup("port"); p != nil && p.Changed {
73+
portProvidedFromFlag = true
74+
}
75+
runMonitorCmd(&portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw, portProvidedFromFlag)
6776
},
6877
}
6978
portArgs.AddToCommand(monitorCommand)
79+
profileArg.AddToCommand(monitorCommand)
7080
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
7181
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
7282
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
7383
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
7484
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
75-
monitorCommand.Flags().StringVarP(&sketchPath, "sketch", "s", "", tr("Path to the sketch"))
76-
fqbn.AddToCommand(monitorCommand)
85+
fqbnArg.AddToCommand(monitorCommand)
7786
return monitorCommand
7887
}
7988

80-
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool, sketchPath string) {
81-
instance := instance.CreateAndInit()
89+
func runMonitorCmd(
90+
portArgs *arguments.Port, fqbnArg *arguments.Fqbn, profileArg *arguments.Profile, sketchPathArg string,
91+
configs []string, describe, timestamp, quiet, raw bool, portProvidedFromFlag bool,
92+
) {
8293
logrus.Info("Executing `arduino-cli monitor`")
8394

8495
if !configuration.HasConsole {
8596
quiet = true
8697
}
8798

88-
addressDefault := ""
89-
protocolDefault := ""
90-
if sketchPath != "" {
91-
sketch, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath})
99+
var (
100+
inst *rpc.Instance
101+
sketchPath *paths.Path
102+
defaultFQBN, defaultPort, defaultProtocol string
103+
)
104+
if !portProvidedFromFlag {
105+
sketchPath = arguments.InitSketchPath(sketchPathArg)
106+
sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
92107
if err != nil {
93-
feedback.FatalError(err, feedback.ErrGeneric)
108+
feedback.Fatal(
109+
tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", err),
110+
feedback.ErrGeneric,
111+
)
94112
}
95-
addressDefault = sketch.GetDefaultPort()
96-
protocolDefault = sketch.GetDefaultProtocol()
97-
}
98-
portAddress, portProtocol, err := portArgs.GetPortAddressAndProtocol(instance, addressDefault, protocolDefault)
99-
if err != nil {
100-
feedback.FatalError(err, feedback.ErrGeneric)
113+
114+
var profile *rpc.Profile
115+
if profileArg.Get() == "" {
116+
inst, profile = instance.CreateAndInitWithProfile(sketch.GetDefaultProfile().GetName(), sketchPath)
117+
} else {
118+
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
119+
}
120+
if fqbnArg.String() == "" {
121+
fqbnArg.Set(profile.GetFqbn())
122+
}
123+
124+
defaultFQBN = sketch.GetDefaultFqbn()
125+
defaultPort = sketch.GetDefaultPort()
126+
defaultProtocol = sketch.GetDefaultProtocol()
127+
} else {
128+
inst = instance.CreateAndInit()
101129
}
130+
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, inst, defaultFQBN, defaultPort, defaultProtocol)
131+
portAddress, portProtocol := port.GetAddress(), port.GetProtocol()
102132

103133
enumerateResp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{
104-
Instance: instance,
134+
Instance: inst,
105135
PortProtocol: portProtocol,
106-
Fqbn: fqbn.String(),
136+
Fqbn: fqbn,
107137
})
108138
if err != nil {
109139
feedback.Fatal(tr("Error getting port settings details: %s", err), feedback.ErrGeneric)
@@ -155,9 +185,9 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
155185
}
156186
}
157187
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
158-
Instance: instance,
188+
Instance: inst,
159189
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
160-
Fqbn: fqbn.String(),
190+
Fqbn: fqbn,
161191
PortConfiguration: configuration,
162192
})
163193
if err != nil {

0 commit comments

Comments
 (0)