Skip to content

Commit

Permalink
remove sketch flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Oct 18, 2023
1 parent 41c4c56 commit f45c9fc
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
"time"

"github.com/arduino/arduino-cli/commands/monitor"
"github.com/arduino/arduino-cli/commands/sketch"
sk "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"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/cli/instance"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/table"
"github.com/arduino/go-paths-helper"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -46,14 +47,14 @@ var tr = i18n.Tr
// NewCommand created a new `monitor` command
func NewCommand() *cobra.Command {
var (
raw bool
portArgs arguments.Port
fqbnArg arguments.Fqbn
profileArg arguments.Profile
raw bool
describe bool
configs []string
quiet bool
timestamp bool
fqbn arguments.Fqbn
sketchPath string
)
monitorCommand := &cobra.Command{
Use: "monitor",
Expand All @@ -63,47 +64,76 @@ 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, timestamp, quiet, raw, sketchPath)
sketchPath := ""
if len(args) > 0 {
sketchPath = args[0]
}
var portProvidedFromFlag bool
if p := cmd.Flags().Lookup("port"); p != nil && p.Changed {
portProvidedFromFlag = true
}
runMonitorCmd(&portArgs, &fqbnArg, &profileArg, sketchPath, configs, describe, timestamp, quiet, raw, portProvidedFromFlag)
},
}
portArgs.AddToCommand(monitorCommand)
profileArg.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().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
monitorCommand.Flags().StringVarP(&sketchPath, "sketch", "s", "", tr("Path to the sketch"))
fqbn.AddToCommand(monitorCommand)
fqbnArg.AddToCommand(monitorCommand)
return monitorCommand
}

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

if !configuration.HasConsole {
quiet = true
}

addressDefault := ""
protocolDefault := ""
if sketchPath != "" {
sketch, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath})
var (
inst *rpc.Instance
sketchPath *paths.Path
defaultFQBN, defaultPort, defaultProtocol string
)
if !portProvidedFromFlag {
sketchPath = arguments.InitSketchPath(sketchPathArg)
sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
feedback.Fatal(
tr("Error getting default port from `sketch.yaml`. Check if you're in the correct sketch folder or provide the --port flag: %s", 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)

var profile *rpc.Profile
if profileArg.Get() == "" {
inst, profile = instance.CreateAndInitWithProfile(sketch.GetDefaultProfile().GetName(), sketchPath)
} else {
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
}
if fqbnArg.String() == "" {
fqbnArg.Set(profile.GetFqbn())
}

defaultFQBN = sketch.GetDefaultFqbn()
defaultPort = sketch.GetDefaultPort()
defaultProtocol = sketch.GetDefaultProtocol()
} else {
inst = instance.CreateAndInit()
}
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, inst, defaultFQBN, defaultPort, defaultProtocol)
portAddress, portProtocol := port.GetAddress(), port.GetProtocol()

enumerateResp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{
Instance: instance,
Instance: inst,
PortProtocol: portProtocol,
Fqbn: fqbn.String(),
Fqbn: fqbn,
})
if err != nil {
feedback.Fatal(tr("Error getting port settings details: %s", err), feedback.ErrGeneric)
Expand Down Expand Up @@ -155,9 +185,9 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
}
}
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
Instance: instance,
Instance: inst,
Port: &rpc.Port{Address: portAddress, Protocol: portProtocol},
Fqbn: fqbn.String(),
Fqbn: fqbn,
PortConfiguration: configuration,
})
if err != nil {
Expand Down

0 comments on commit f45c9fc

Please sign in to comment.