forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
monitor --describe
command (arduino#1493)
* Resolve monitors from platform.txt * Implementation of monitor --describe command * fix i18n
- Loading branch information
Showing
10 changed files
with
409 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package monitor | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/arguments" | ||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"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/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/arduino/arduino-cli/table" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var tr = i18n.Tr | ||
|
||
var portArgs arguments.Port | ||
var describe bool | ||
|
||
// NewCommand created a new `monitor` command | ||
func NewCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "monitor", | ||
Short: tr("Open a communication port with a board."), | ||
Long: tr("Open a communication port with a board."), | ||
Example: "" + | ||
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" + | ||
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe", | ||
Run: runMonitorCmd, | ||
} | ||
portArgs.AddToCommand(cmd) | ||
cmd.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port.")) | ||
cmd.MarkFlagRequired("port") | ||
return cmd | ||
} | ||
|
||
func runMonitorCmd(cmd *cobra.Command, args []string) { | ||
instance := instance.CreateAndInit() | ||
|
||
port, err := portArgs.GetPort(instance, nil) | ||
if err != nil { | ||
feedback.Error(err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
if describe { | ||
res, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{ | ||
Instance: instance, | ||
Port: port.ToRPC(), | ||
Fqbn: "", | ||
}) | ||
if err != nil { | ||
feedback.Error(tr("Error getting port settings details: %s"), err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
feedback.PrintResult(&detailsResult{Settings: res.Settings}) | ||
return | ||
} | ||
|
||
feedback.Error("Monitor functionality not yet implemented") | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
type detailsResult struct { | ||
Settings []*rpc.MonitorPortSettingDescriptor `json:"settings"` | ||
} | ||
|
||
func (r *detailsResult) Data() interface{} { | ||
return r | ||
} | ||
|
||
func (r *detailsResult) String() string { | ||
t := table.New() | ||
green := color.New(color.FgGreen) | ||
t.SetHeader(tr("ID"), tr("Setting"), tr("Default"), tr("Values")) | ||
sort.Slice(r.Settings, func(i, j int) bool { | ||
return r.Settings[i].Label < r.Settings[j].Label | ||
}) | ||
for _, setting := range r.Settings { | ||
values := strings.Join(setting.EnumValues, ", ") | ||
t.AddRow(setting.SettingId, setting.Label, table.NewCell(setting.Value, green), values) | ||
} | ||
return t.Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package monitor | ||
|
||
import ( | ||
"github.com/arduino/arduino-cli/arduino/cores" | ||
"github.com/arduino/arduino-cli/arduino/cores/packagemanager" | ||
"github.com/arduino/arduino-cli/commands" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
) | ||
|
||
func findMonitorForProtocolAndBoard(pm *packagemanager.PackageManager, port *rpc.Port, fqbn string) (*cores.MonitorDependency, error) { | ||
if port == nil { | ||
return nil, &commands.MissingPortError{} | ||
} | ||
protocol := port.GetProtocol() | ||
if protocol == "" { | ||
return nil, &commands.MissingPortProtocolError{} | ||
} | ||
|
||
// If a board is specified search the monitor in the board package first | ||
if fqbn != "" { | ||
fqbn, err := cores.ParseFQBN(fqbn) | ||
if err != nil { | ||
return nil, &commands.InvalidFQBNError{Cause: err} | ||
} | ||
|
||
_, boardPlatform, _, _, _, err := pm.ResolveFQBN(fqbn) | ||
if err != nil { | ||
return nil, &commands.UnknownFQBNError{Cause: err} | ||
} | ||
if mon, ok := boardPlatform.Monitors[protocol]; ok { | ||
return mon, nil | ||
} | ||
} | ||
|
||
// Otherwise look in all package for a suitable monitor | ||
for _, platformRel := range pm.InstalledPlatformReleases() { | ||
if mon, ok := platformRel.Monitors[protocol]; ok { | ||
return mon, nil | ||
} | ||
} | ||
return nil, &commands.NoMonitorAvailableForProtocolError{Protocol: protocol} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package monitor | ||
|
||
import ( | ||
"context" | ||
|
||
pluggableMonitor "github.com/arduino/arduino-cli/arduino/monitor" | ||
"github.com/arduino/arduino-cli/commands" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
) | ||
|
||
// EnumerateMonitorPortSettings returns a description of the configuration settings of a monitor port | ||
func EnumerateMonitorPortSettings(ctx context.Context, req *rpc.EnumerateMonitorPortSettingsRequest) (*rpc.EnumerateMonitorPortSettingsResponse, error) { | ||
pm := commands.GetPackageManager(req.GetInstance().GetId()) | ||
if pm == nil { | ||
return nil, &commands.InvalidInstanceError{} | ||
} | ||
|
||
monitorRef, err := findMonitorForProtocolAndBoard(pm, req.GetPort(), req.GetFqbn()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tool := pm.FindMonitorDependency(monitorRef) | ||
if tool == nil { | ||
return nil, &commands.MonitorNotFoundError{Monitor: monitorRef.String()} | ||
} | ||
|
||
m := pluggableMonitor.New(monitorRef.Name, tool.InstallDir.Join(monitorRef.Name).String()) | ||
|
||
if err := m.Run(); err != nil { | ||
return nil, &commands.FailedMonitorError{Cause: err} | ||
} | ||
defer m.Quit() | ||
|
||
desc, err := m.Describe() | ||
if err != nil { | ||
return nil, &commands.FailedMonitorError{Cause: err} | ||
} | ||
return &rpc.EnumerateMonitorPortSettingsResponse{Settings: convert(desc)}, nil | ||
} | ||
|
||
func convert(desc *pluggableMonitor.PortDescriptor) []*rpc.MonitorPortSettingDescriptor { | ||
res := []*rpc.MonitorPortSettingDescriptor{} | ||
for settingID, descriptor := range desc.ConfigurationParameters { | ||
res = append(res, &rpc.MonitorPortSettingDescriptor{ | ||
SettingId: settingID, | ||
Label: descriptor.Label, | ||
Type: descriptor.Type, | ||
EnumValues: descriptor.Values, | ||
Value: descriptor.Selected, | ||
}) | ||
} | ||
return res | ||
} |
Oops, something went wrong.