Skip to content

[pluggable monitor] add --silent and --config flags #1497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package monitor
import (
"context"
"errors"
"fmt"
"io"
"os"
"sort"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/monitor"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/table"
Expand All @@ -39,6 +41,8 @@ var tr = i18n.Tr

var portArgs arguments.Port
var describe bool
var configs []string
var quiet bool

// NewCommand created a new `monitor` command
func NewCommand() *cobra.Command {
Expand All @@ -53,13 +57,19 @@ func NewCommand() *cobra.Command {
}
portArgs.AddToCommand(cmd)
cmd.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
cmd.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configuration of the port."))
cmd.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
cmd.MarkFlagRequired("port")
return cmd
}

func runMonitorCmd(cmd *cobra.Command, args []string) {
instance := instance.CreateAndInit()

if !configuration.HasConsole {
quiet = true
}

port, err := portArgs.GetPort(instance, nil)
if err != nil {
feedback.Error(err)
Expand Down Expand Up @@ -87,10 +97,63 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
}
defer tty.Close()

configuration := &rpc.MonitorPortConfiguration{}
if len(configs) > 0 {
resp, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{
Instance: instance,
Port: port.ToRPC(),
Fqbn: "",
})
if err != nil {
feedback.Error(err)
os.Exit(errorcodes.ErrGeneric)
}
for _, config := range configs {
split := strings.SplitN(config, "=", 2)
k := ""
v := config
if len(split) == 2 {
k = split[0]
v = split[1]
}

var setting *rpc.MonitorPortSettingDescriptor
for _, s := range resp.GetSettings() {
if k == "" {
if contains(s.EnumValues, v) {
setting = s
break
}
} else {
if strings.EqualFold(s.SettingId, k) {
if !contains(s.EnumValues, v) {
feedback.Error(tr("invalid port configuration value for %s: %s", k, v))
os.Exit(errorcodes.ErrBadArgument)
}
setting = s
break
}
}
}
if setting == nil {
feedback.Error(tr("invalid port configuration: %s", config))
os.Exit(errorcodes.ErrBadArgument)
}
configuration.Settings = append(configuration.Settings, &rpc.MonitorPortSetting{
SettingId: setting.SettingId,
Value: v,
})
if !quiet {
feedback.Print(tr("Monitor port settings:"))
feedback.Print(fmt.Sprintf("%s=%s", setting.SettingId, v))
}
}
}
portProxy, _, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
Instance: instance,
Port: port.ToRPC(),
Fqbn: "",
Instance: instance,
Port: port.ToRPC(),
Fqbn: "",
PortConfiguration: configuration,
})
if err != nil {
feedback.Error(err)
Expand All @@ -114,7 +177,9 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
cancel()
}()

feedback.Print(tr("Connected to %s! Press CTRL-C to exit.", port.String()))
if !quiet {
feedback.Print(tr("Connected to %s! Press CTRL-C to exit.", port.String()))
}

// Wait for port closed
<-ctx.Done()
Expand All @@ -141,3 +206,12 @@ func (r *detailsResult) String() string {
}
return t.Render()
}

func contains(s []string, searchterm string) bool {
for _, item := range s {
if strings.EqualFold(item, searchterm) {
return true
}
}
return false
}
42 changes: 31 additions & 11 deletions i18n/data/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ msgstr "Config file already exists, use --overwrite to discard the existing one.
msgid "Config file written to: %s"
msgstr "Config file written to: %s"

#: cli/monitor/monitor.go:60
msgid "Configuration of the port."
msgstr "Configuration of the port."

#: cli/debug/debug.go:153
msgid "Configuration options for %s"
msgstr "Configuration options for %s"
Expand All @@ -423,7 +427,7 @@ msgstr "Configuring platform."
msgid "Connected"
msgstr "Connected"

#: cli/monitor/monitor.go:117
#: cli/monitor/monitor.go:181
msgid "Connected to %s! Press CTRL-C to exit."
msgstr "Connected to %s! Press CTRL-C to exit."

Expand Down Expand Up @@ -500,7 +504,7 @@ msgstr "Debugging not supported for board %s"
msgid "Debugging supported:"
msgstr "Debugging supported:"

#: cli/monitor/monitor.go:134
#: cli/monitor/monitor.go:199
msgid "Default"
msgstr "Default"

Expand Down Expand Up @@ -786,7 +790,7 @@ msgstr "Error getting information for library %s"
msgid "Error getting libraries info: %v"
msgstr "Error getting libraries info: %v"

#: cli/monitor/monitor.go:76
#: cli/monitor/monitor.go:86
msgid "Error getting port settings details: %s"
msgstr "Error getting port settings details: %s"

Expand Down Expand Up @@ -1134,7 +1138,7 @@ msgstr "Global variables use {0} bytes of dynamic memory."

#: cli/core/list.go:84
#: cli/core/search.go:114
#: cli/monitor/monitor.go:134
#: cli/monitor/monitor.go:199
#: cli/outdated/outdated.go:62
msgid "ID"
msgstr "ID"
Expand Down Expand Up @@ -1471,6 +1475,10 @@ msgstr "Missing sketch path"
msgid "Monitor '%s' not found"
msgstr "Monitor '%s' not found"

#: cli/monitor/monitor.go:147
msgid "Monitor port settings:"
msgstr "Monitor port settings:"

#: legacy/builder/print_used_and_not_used_libraries.go:50
msgid "Multiple libraries were found for \"{0}\""
msgstr "Multiple libraries were found for \"{0}\""
Expand Down Expand Up @@ -1572,8 +1580,8 @@ msgstr "OS:"
msgid "Official Arduino board:"
msgstr "Official Arduino board:"

#: cli/monitor/monitor.go:47
#: cli/monitor/monitor.go:48
#: cli/monitor/monitor.go:51
#: cli/monitor/monitor.go:52
msgid "Open a communication port with a board."
msgstr "Open a communication port with a board."

Expand Down Expand Up @@ -1723,8 +1731,8 @@ msgstr "Platform size (bytes):"
msgid "Port"
msgstr "Port"

#: cli/monitor/monitor.go:105
#: cli/monitor/monitor.go:112
#: cli/monitor/monitor.go:168
#: cli/monitor/monitor.go:175
msgid "Port closed:"
msgstr "Port closed:"

Expand Down Expand Up @@ -1807,6 +1815,10 @@ msgstr "Required tool:"
msgid "Run as a daemon on port: %s"
msgstr "Run as a daemon on port: %s"

#: cli/monitor/monitor.go:61
msgid "Run in silent mode, show only monitor input and output."
msgstr "Run in silent mode, show only monitor input and output."

#: cli/daemon/daemon.go:51
msgid "Running as a daemon the initialization of cores and libraries is done only once."
msgstr "Running as a daemon the initialization of cores and libraries is done only once."
Expand Down Expand Up @@ -1861,7 +1873,7 @@ msgstr "Sets a setting value."
msgid "Sets where to save the configuration file."
msgstr "Sets where to save the configuration file."

#: cli/monitor/monitor.go:134
#: cli/monitor/monitor.go:199
msgid "Setting"
msgstr "Setting"

Expand All @@ -1878,7 +1890,7 @@ msgstr "Show all available core versions."
msgid "Show all build properties used instead of compiling."
msgstr "Show all build properties used instead of compiling."

#: cli/monitor/monitor.go:55
#: cli/monitor/monitor.go:59
msgid "Show all the settings of the communication port."
msgstr "Show all the settings of the communication port."

Expand Down Expand Up @@ -2336,7 +2348,7 @@ msgstr "VERSION"
msgid "VERSION_NUMBER"
msgstr "VERSION_NUMBER"

#: cli/monitor/monitor.go:134
#: cli/monitor/monitor.go:199
msgid "Values"
msgstr "Values"

Expand Down Expand Up @@ -2843,6 +2855,14 @@ msgstr "invalid platform archive size: %s"
msgid "invalid pluggable monitor reference: %s"
msgstr "invalid pluggable monitor reference: %s"

#: cli/monitor/monitor.go:130
msgid "invalid port configuration value for %s: %s"
msgstr "invalid port configuration value for %s: %s"

#: cli/monitor/monitor.go:139
msgid "invalid port configuration: %s"
msgstr "invalid port configuration: %s"

#: commands/upload/upload.go:483
msgid "invalid recipe '%[1]s': %[2]s"
msgstr "invalid recipe '%[1]s': %[2]s"
Expand Down
8 changes: 4 additions & 4 deletions i18n/rice-box.go

Large diffs are not rendered by default.