Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/arduino-app-cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func NewAppCmd(cfg config.Configuration) *cobra.Command {
appCmd.AddCommand(newLogsCmd(cfg))
appCmd.AddCommand(newListCmd(cfg))
appCmd.AddCommand(newPsCmd())
appCmd.AddCommand(newMonitorCmd(cfg))

return appCmd
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/arduino-app-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/config"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/daemon"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/monitor"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/properties"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/system"
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/version"
Expand Down Expand Up @@ -78,6 +79,7 @@ func run(configuration cfg.Configuration) error {
config.NewConfigCmd(configuration),
system.NewSystemCmd(configuration),
version.NewVersionCmd(Version),
monitor.NewMonitorCmd(),
)

ctx := context.Background()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,51 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package app
package monitor

import (
"io"
"os"

"github.com/spf13/cobra"

"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion"
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
"github.com/arduino/arduino-app-cli/internal/api/handlers"
)

func newMonitorCmd(cfg config.Configuration) *cobra.Command {
func NewMonitorCmd() *cobra.Command {
return &cobra.Command{
Use: "monitor",
Short: "Monitor the Arduino app",
RunE: func(cmd *cobra.Command, args []string) error {
panic("not implemented")
err := handlers.HandlerMonitor(&stdInOutProxy{stdin: os.Stdin, stdout: os.Stdout}) // nolint:forbidigo
if err != nil {
return err
}
<-cmd.Context().Done()
return nil
},
ValidArgsFunction: completion.ApplicationNames(cfg),
}
}

type stdInOutProxy struct {
stdin io.Reader
stdout io.Writer
}

func (s stdInOutProxy) ReadMessage() (int, []byte, error) {
var p [1024]byte
n, err := s.stdin.Read(p[:])
if err != nil {
return 0, nil, err
}
return 1, p[:n], nil
}

func (s stdInOutProxy) WriteMessage(messageType int, data []byte) error {
_, err := s.stdout.Write(data)
return err
}

func (s stdInOutProxy) Close() error {
return nil
}
20 changes: 19 additions & 1 deletion internal/api/handlers/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,25 @@ import (
"github.com/arduino/arduino-app-cli/internal/render"
)

func monitorStream(mon net.Conn, ws *websocket.Conn) {
type MessageReaderWriter interface {
ReadMessage() (messageType int, p []byte, err error)
WriteMessage(messageType int, data []byte) error
Close() error
}

// TODO: move this in internal/orchestrator/monitor
func HandlerMonitor(ws MessageReaderWriter) error {
// Connect to monitor
mon, err := net.DialTimeout("tcp", "127.0.0.1:7500", time.Second)
if err != nil {
return err
}

monitorStream(mon, ws)
return nil
}

func monitorStream(mon net.Conn, ws MessageReaderWriter) {
logWebsocketError := func(msg string, err error) {
// Do not log simple close or interruption errors
if websocket.IsUnexpectedCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived, websocket.CloseAbnormalClosure) {
Expand Down