forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor.go
105 lines (92 loc) · 3.12 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()
}