@@ -18,6 +18,7 @@ package monitor
1818import (
1919 "context"
2020 "errors"
21+ "fmt"
2122 "io"
2223 "os"
2324 "sort"
@@ -28,6 +29,7 @@ import (
2829 "github.com/arduino/arduino-cli/cli/feedback"
2930 "github.com/arduino/arduino-cli/cli/instance"
3031 "github.com/arduino/arduino-cli/commands/monitor"
32+ "github.com/arduino/arduino-cli/configuration"
3133 "github.com/arduino/arduino-cli/i18n"
3234 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3335 "github.com/arduino/arduino-cli/table"
@@ -39,6 +41,7 @@ var tr = i18n.Tr
3941
4042var portArgs arguments.Port
4143var describe bool
44+ var configs []string
4245var silent bool
4346
4447// NewCommand created a new `monitor` command
@@ -54,6 +57,7 @@ func NewCommand() *cobra.Command {
5457 }
5558 portArgs .AddToCommand (cmd )
5659 cmd .Flags ().BoolVar (& describe , "describe" , false , tr ("Show all the settings of the communication port." ))
60+ cmd .Flags ().StringSliceVarP (& configs , "config" , "c" , []string {}, tr ("Configuration of the port." ))
5761 cmd .Flags ().BoolVarP (& silent , "silent" , "s" , false , tr ("Run in silent mode, show only monitor input and output." ))
5862 cmd .MarkFlagRequired ("port" )
5963 return cmd
@@ -93,10 +97,63 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
9397 }
9498 defer tty .Close ()
9599
100+ configuration := & rpc.MonitorPortConfiguration {}
101+ if len (configs ) > 0 {
102+ resp , err := monitor .EnumerateMonitorPortSettings (context .Background (), & rpc.EnumerateMonitorPortSettingsRequest {
103+ Instance : instance ,
104+ Port : port .ToRPC (),
105+ Fqbn : "" ,
106+ })
107+ if err != nil {
108+ feedback .Error (err )
109+ os .Exit (errorcodes .ErrGeneric )
110+ }
111+ settings := resp .GetSettings ()
112+ for _ , config := range configs {
113+ split := strings .SplitN (config , "=" , 2 )
114+ k := ""
115+ v := config
116+ if len (split ) == 2 {
117+ k = split [0 ]
118+ v = split [1 ]
119+ }
120+
121+ var setting * rpc.MonitorPortSettingDescriptor
122+ for _ , s := range settings {
123+ if k == "" {
124+ if contains (s .EnumValues , v ) {
125+ setting = s
126+ break
127+ }
128+ } else {
129+ if strings .EqualFold (s .SettingId , k ) {
130+ if ! contains (s .EnumValues , v ) {
131+ feedback .Error (tr ("invalid port configuration value for %s: %s" , k , v ))
132+ os .Exit (errorcodes .ErrBadArgument )
133+ }
134+ setting = s
135+ break
136+ }
137+ }
138+ }
139+ if setting == nil {
140+ feedback .Error (tr ("invalid port configuration: %s" , config ))
141+ os .Exit (errorcodes .ErrBadArgument )
142+ }
143+ configuration .Settings = append (configuration .Settings , & rpc.MonitorPortSetting {
144+ SettingId : setting .SettingId ,
145+ Value : v ,
146+ })
147+ if ! silent {
148+ feedback .Print (fmt .Sprintf ("Set %s to %s" , setting .SettingId , v ))
149+ }
150+ }
151+ }
96152 portProxy , _ , err := monitor .Monitor (context .Background (), & rpc.MonitorRequest {
97- Instance : instance ,
98- Port : port .ToRPC (),
99- Fqbn : "" ,
153+ Instance : instance ,
154+ Port : port .ToRPC (),
155+ Fqbn : "" ,
156+ PortConfiguration : configuration ,
100157 })
101158 if err != nil {
102159 feedback .Error (err )
@@ -149,3 +206,12 @@ func (r *detailsResult) String() string {
149206 }
150207 return t .Render ()
151208}
209+
210+ func contains (s []string , searchterm string ) bool {
211+ for _ , item := range s {
212+ if strings .EqualFold (item , searchterm ) {
213+ return true
214+ }
215+ }
216+ return false
217+ }
0 commit comments