Skip to content

Commit 1fcfbd5

Browse files
committed
feat: add bridge monitor command
1 parent 5be93c5 commit 1fcfbd5

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

cmd/arduino-app-cli/app/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func NewAppCmd(cfg config.Configuration) *cobra.Command {
3939
appCmd.AddCommand(newLogsCmd(cfg))
4040
appCmd.AddCommand(newListCmd(cfg))
4141
appCmd.AddCommand(newPsCmd())
42-
appCmd.AddCommand(newMonitorCmd(cfg))
4342

4443
return appCmd
4544
}

cmd/arduino-app-cli/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/config"
3131
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/daemon"
3232
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
33+
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/monitor"
3334
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/properties"
3435
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/system"
3536
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/version"
@@ -78,6 +79,7 @@ func run(configuration cfg.Configuration) error {
7879
config.NewConfigCmd(configuration),
7980
system.NewSystemCmd(configuration),
8081
version.NewVersionCmd(Version),
82+
monitor.NewMonitorCmd(),
8183
)
8284

8385
ctx := context.Background()

cmd/arduino-app-cli/app/monitor.go renamed to cmd/arduino-app-cli/monitor/monitor.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,51 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package app
16+
package monitor
1717

1818
import (
19+
"io"
20+
"os"
21+
1922
"github.com/spf13/cobra"
2023

21-
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
22-
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
24+
"github.com/arduino/arduino-app-cli/internal/api/handlers"
2325
)
2426

25-
func newMonitorCmd(cfg config.Configuration) *cobra.Command {
27+
func NewMonitorCmd() *cobra.Command {
2628
return &cobra.Command{
2729
Use: "monitor",
2830
Short: "Monitor the Arduino app",
2931
RunE: func(cmd *cobra.Command, args []string) error {
30-
panic("not implemented")
32+
err := handlers.HandlerMonitor(&stdInOutProxy{stdin: os.Stdin, stdout: os.Stdout}) // nolint:forbidigo
33+
if err != nil {
34+
return err
35+
}
36+
<-cmd.Context().Done()
37+
return nil
3138
},
32-
ValidArgsFunction: completion.ApplicationNames(cfg),
3339
}
3440
}
41+
42+
type stdInOutProxy struct {
43+
stdin io.Reader
44+
stdout io.Writer
45+
}
46+
47+
func (s stdInOutProxy) ReadMessage() (int, []byte, error) {
48+
var p [1024]byte
49+
n, err := s.stdin.Read(p[:])
50+
if err != nil {
51+
return 0, nil, err
52+
}
53+
return 1, p[:n], nil
54+
}
55+
56+
func (s stdInOutProxy) WriteMessage(messageType int, data []byte) error {
57+
_, err := s.stdout.Write(data)
58+
return err
59+
}
60+
61+
func (s stdInOutProxy) Close() error {
62+
return nil
63+
}

internal/api/handlers/monitor.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,25 @@ import (
3131
"github.com/arduino/arduino-app-cli/internal/render"
3232
)
3333

34-
func monitorStream(mon net.Conn, ws *websocket.Conn) {
34+
type MessageReaderWriter interface {
35+
ReadMessage() (messageType int, p []byte, err error)
36+
WriteMessage(messageType int, data []byte) error
37+
Close() error
38+
}
39+
40+
// TODO: move this in internal/orchestrator/monitor
41+
func HandlerMonitor(ws MessageReaderWriter) error {
42+
// Connect to monitor
43+
mon, err := net.DialTimeout("tcp", "127.0.0.1:7500", time.Second)
44+
if err != nil {
45+
return err
46+
}
47+
48+
monitorStream(mon, ws)
49+
return nil
50+
}
51+
52+
func monitorStream(mon net.Conn, ws MessageReaderWriter) {
3553
logWebsocketError := func(msg string, err error) {
3654
// Do not log simple close or interruption errors
3755
if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived, websocket.CloseAbnormalClosure) {

0 commit comments

Comments
 (0)