|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package monitor |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "sort" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/cli/arguments" |
| 25 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 26 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 27 | + "github.com/arduino/arduino-cli/cli/instance" |
| 28 | + "github.com/arduino/arduino-cli/commands/monitor" |
| 29 | + "github.com/arduino/arduino-cli/i18n" |
| 30 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 31 | + "github.com/arduino/arduino-cli/table" |
| 32 | + "github.com/fatih/color" |
| 33 | + "github.com/spf13/cobra" |
| 34 | +) |
| 35 | + |
| 36 | +var tr = i18n.Tr |
| 37 | + |
| 38 | +var portArgs arguments.Port |
| 39 | +var describe bool |
| 40 | + |
| 41 | +// NewCommand created a new `monitor` command |
| 42 | +func NewCommand() *cobra.Command { |
| 43 | + cmd := &cobra.Command{ |
| 44 | + Use: "monitor", |
| 45 | + Short: tr("Open a communication port with a board."), |
| 46 | + Long: tr("Open a communication port with a board."), |
| 47 | + Example: "" + |
| 48 | + " " + os.Args[0] + " monitor -p /dev/ttyACM0\n" + |
| 49 | + " " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe", |
| 50 | + Run: runMonitorCmd, |
| 51 | + } |
| 52 | + portArgs.AddToCommand(cmd) |
| 53 | + cmd.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port.")) |
| 54 | + cmd.MarkFlagRequired("port") |
| 55 | + return cmd |
| 56 | +} |
| 57 | + |
| 58 | +func runMonitorCmd(cmd *cobra.Command, args []string) { |
| 59 | + instance := instance.CreateAndInit() |
| 60 | + |
| 61 | + port, err := portArgs.GetPort(instance, nil) |
| 62 | + if err != nil { |
| 63 | + feedback.Error(err) |
| 64 | + os.Exit(errorcodes.ErrGeneric) |
| 65 | + } |
| 66 | + |
| 67 | + if describe { |
| 68 | + res, err := monitor.EnumerateMonitorPortSettings(context.Background(), &rpc.EnumerateMonitorPortSettingsRequest{ |
| 69 | + Instance: instance, |
| 70 | + Port: port.ToRPC(), |
| 71 | + Fqbn: "", |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + feedback.Error(tr("Error getting port settings details: %s"), err) |
| 75 | + os.Exit(errorcodes.ErrGeneric) |
| 76 | + } |
| 77 | + feedback.PrintResult(&detailsResult{Settings: res.Settings}) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + feedback.Error("Monitor functionality not yet implemented") |
| 82 | + os.Exit(errorcodes.ErrGeneric) |
| 83 | +} |
| 84 | + |
| 85 | +type detailsResult struct { |
| 86 | + Settings []*rpc.MonitorPortSettingDescriptor `json:"settings"` |
| 87 | +} |
| 88 | + |
| 89 | +func (r *detailsResult) Data() interface{} { |
| 90 | + return r |
| 91 | +} |
| 92 | + |
| 93 | +func (r *detailsResult) String() string { |
| 94 | + t := table.New() |
| 95 | + green := color.New(color.FgGreen) |
| 96 | + t.SetHeader(tr("ID"), tr("Setting"), tr("Default"), tr("Values")) |
| 97 | + sort.Slice(r.Settings, func(i, j int) bool { |
| 98 | + return r.Settings[i].Label < r.Settings[j].Label |
| 99 | + }) |
| 100 | + for _, setting := range r.Settings { |
| 101 | + values := strings.Join(setting.EnumValues, ", ") |
| 102 | + t.AddRow(setting.SettingId, setting.Label, table.NewCell(setting.Value, green), values) |
| 103 | + } |
| 104 | + return t.Render() |
| 105 | +} |
0 commit comments