Skip to content

Commit 736d201

Browse files
committed
Implementation of 'monitor' command of the CLI
1 parent 70d8c00 commit 736d201

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

Diff for: cli/monitor/monitor.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package monitor
1717

1818
import (
1919
"context"
20+
"errors"
21+
"io"
2022
"os"
2123
"sort"
2224
"strings"
@@ -78,8 +80,50 @@ func runMonitorCmd(cmd *cobra.Command, args []string) {
7880
return
7981
}
8082

81-
feedback.Error("Monitor functionality not yet implemented")
82-
os.Exit(errorcodes.ErrGeneric)
83+
tty, err := newStdInOutTerminal()
84+
if err != nil {
85+
feedback.Error(err)
86+
os.Exit(errorcodes.ErrGeneric)
87+
}
88+
defer tty.Close()
89+
90+
portProxy, descriptor, err := monitor.Monitor(context.Background(), &rpc.MonitorRequest{
91+
Instance: instance,
92+
Port: port.ToRPC(),
93+
Fqbn: "",
94+
})
95+
if err != nil {
96+
feedback.Error(err)
97+
os.Exit(errorcodes.ErrGeneric)
98+
}
99+
defer portProxy.Close()
100+
101+
ctx, cancel := context.WithCancel(context.Background())
102+
go func() {
103+
_, err := io.Copy(tty, portProxy)
104+
if err != nil && !errors.Is(err, io.EOF) {
105+
feedback.Error(tr("Port closed:"), err)
106+
}
107+
cancel()
108+
}()
109+
go func() {
110+
_, err := io.Copy(portProxy, tty)
111+
if err != nil && !errors.Is(err, io.EOF) {
112+
feedback.Error(tr("Port closed:"), err)
113+
}
114+
cancel()
115+
}()
116+
117+
params := descriptor.ConfigurationParameters
118+
paramsKeys := sort.StringSlice{}
119+
for k := range params {
120+
paramsKeys = append(paramsKeys, k)
121+
}
122+
sort.Sort(paramsKeys)
123+
feedback.Print(tr("Connected to %s! Press CTRL-C to exit.", port.String()))
124+
125+
// Wait for port closed
126+
<-ctx.Done()
83127
}
84128

85129
type detailsResult struct {

Diff for: cli/monitor/term.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
"os"
20+
)
21+
22+
type stdInOut struct {
23+
}
24+
25+
func newStdInOutTerminal() (*stdInOut, error) {
26+
return &stdInOut{}, nil
27+
}
28+
29+
func (n *stdInOut) Close() error {
30+
return nil
31+
}
32+
33+
func (n *stdInOut) Read(buff []byte) (int, error) {
34+
return os.Stdin.Read(buff)
35+
}
36+
37+
func (n *stdInOut) Write(buff []byte) (int, error) {
38+
return os.Stdout.Write(buff)
39+
}

0 commit comments

Comments
 (0)