diff --git a/commands/apps.go b/commands/apps.go index fbac11dd8..2cd073e55 100644 --- a/commands/apps.go +++ b/commands/apps.go @@ -765,7 +765,7 @@ func RunAppsConsole(c *CmdConfig) error { resizeEvents := make(chan listen.TerminalSize) grp.Go(func() error { - return listener.MonitorResizeEvents(ctx, int(os.Stdin.Fd()), resizeEvents) + return listener.MonitorResizeEvents(ctx, resizeEvents) }) grp.Go(func() error { diff --git a/commands/apps_test.go b/commands/apps_test.go index e49ea55dd..975940724 100644 --- a/commands/apps_test.go +++ b/commands/apps_test.go @@ -462,7 +462,7 @@ func TestRunAppsConsole(t *testing.T) { tm.apps.EXPECT().GetExec(appID, deploymentID, component).Times(1).Return(&godo.AppExec{URL: "wss://proxy-apps-prod-ams3-001.ondigitalocean.app/?token=aa-bb-11-cc-33"}, nil) tm.listen.EXPECT().Listen(gomock.Any()).Times(1).Return(nil) tm.listen.EXPECT().ReadRawStdin(gomock.Any(), gomock.Any()).Times(1).Return(nil) - tm.listen.EXPECT().MonitorResizeEvents(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(nil) + tm.listen.EXPECT().MonitorResizeEvents(gomock.Any(), gomock.Any()).Times(1).Return(nil) tc := config.Doit.(*doctl.TestConfig) tc.ListenFn = func(url *url.URL, token string, schemaFunc listen.SchemaFunc, out io.Writer, in <-chan []byte) listen.ListenerService { diff --git a/do/mocks/Listen.go b/do/mocks/Listen.go index 9dd920cbd..2a90c667f 100644 --- a/do/mocks/Listen.go +++ b/do/mocks/Listen.go @@ -56,17 +56,17 @@ func (mr *MockListenerServiceMockRecorder) Listen(ctx any) *gomock.Call { } // MonitorResizeEvents mocks base method. -func (m *MockListenerService) MonitorResizeEvents(ctx context.Context, fd int, resizeEvents chan<- listen.TerminalSize) error { +func (m *MockListenerService) MonitorResizeEvents(ctx context.Context, resizeEvents chan<- listen.TerminalSize) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "MonitorResizeEvents", ctx, fd, resizeEvents) + ret := m.ctrl.Call(m, "MonitorResizeEvents", ctx, resizeEvents) ret0, _ := ret[0].(error) return ret0 } // MonitorResizeEvents indicates an expected call of MonitorResizeEvents. -func (mr *MockListenerServiceMockRecorder) MonitorResizeEvents(ctx, fd, resizeEvents any) *gomock.Call { +func (mr *MockListenerServiceMockRecorder) MonitorResizeEvents(ctx, resizeEvents any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MonitorResizeEvents", reflect.TypeOf((*MockListenerService)(nil).MonitorResizeEvents), ctx, fd, resizeEvents) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MonitorResizeEvents", reflect.TypeOf((*MockListenerService)(nil).MonitorResizeEvents), ctx, resizeEvents) } // ReadRawStdin mocks base method. diff --git a/pkg/listen/listen.go b/pkg/listen/listen.go index 68fcc3807..0709f546e 100644 --- a/pkg/listen/listen.go +++ b/pkg/listen/listen.go @@ -38,7 +38,7 @@ type Listener struct { type ListenerService interface { Listen(ctx context.Context) error ReadRawStdin(ctx context.Context, stdinCh chan<- string) error - MonitorResizeEvents(ctx context.Context, fd int, resizeEvents chan<- TerminalSize) error + MonitorResizeEvents(ctx context.Context, resizeEvents chan<- TerminalSize) error } var _ ListenerService = &Listener{} diff --git a/pkg/listen/listen_resize.go b/pkg/listen/listen_resize.go index 63234b943..aab652451 100644 --- a/pkg/listen/listen_resize.go +++ b/pkg/listen/listen_resize.go @@ -14,13 +14,13 @@ import ( ) // MonitorResizeEvents monitors the terminal for resize events and sends them to the provided channel. -func (l *Listener) MonitorResizeEvents(ctx context.Context, fd int, resizeEvents chan<- TerminalSize) error { +func (l *Listener) MonitorResizeEvents(ctx context.Context, resizeEvents chan<- TerminalSize) error { winch := make(chan os.Signal, 1) signal.Notify(winch, unix.SIGWINCH) defer signal.Stop(winch) for { - width, height, err := term.GetSize(fd) + width, height, err := term.GetSize(int(os.Stdin.Fd())) if err != nil { return fmt.Errorf("error getting terminal size: %w", err) } diff --git a/pkg/listen/listen_test.go b/pkg/listen/listen_test.go index 9e781300a..be708b946 100644 --- a/pkg/listen/listen_test.go +++ b/pkg/listen/listen_test.go @@ -112,22 +112,3 @@ func TestListenerWithSchemaFunc(t *testing.T) { ` require.Equal(t, want, buffer.String()) } - -func TestListenerStop(t *testing.T) { - server := httptest.NewServer(wsHandler(t)) - defer server.Close() - - u := "ws" + strings.TrimPrefix(server.URL, "http") - url, err := url.Parse(u) - require.NoError(t, err) - - buffer := &bytes.Buffer{} - - listener := NewListener(url, "", nil, buffer, nil) - ctx, cancel := context.WithCancel(context.Background()) - go listener.Listen(ctx) - // Stop before any messages have been sent - cancel() - - require.Equal(t, "", buffer.String()) -} diff --git a/pkg/listen/listen_windows.go b/pkg/listen/listen_windows.go index 7640b89c4..13118d811 100644 --- a/pkg/listen/listen_windows.go +++ b/pkg/listen/listen_windows.go @@ -3,18 +3,19 @@ package listen import ( "context" "fmt" + "os" "time" "golang.org/x/term" ) // MonitorResizeEvents monitors the terminal for resize events and sends them to the provided channel. -func (l *Listener) MonitorResizeEvents(ctx context.Context, fd int, resizeEvents chan<- TerminalSize) error { +func (l *Listener) MonitorResizeEvents(ctx context.Context, resizeEvents chan<- TerminalSize) error { var prevTerminalSize TerminalSize ticker := time.NewTicker(250 * time.Millisecond) for { - width, height, err := term.GetSize(fd) + width, height, err := term.GetSize(int(os.Stdin.Fd())) if err != nil { return fmt.Errorf("error getting terminal size: %w", err) } diff --git a/util.go b/util.go index 3b3d48907..040dae905 100644 --- a/util.go +++ b/util.go @@ -50,6 +50,6 @@ func (tr *MockListener) ReadRawStdin(ctx context.Context, stdinCh chan<- string) } // MonitorResizeEvents mocks ListenerService.MonitorResizeEvents -func (tr *MockListener) MonitorResizeEvents(ctx context.Context, fd int, resizeEvents chan<- listen.TerminalSize) error { +func (tr *MockListener) MonitorResizeEvents(ctx context.Context, resizeEvents chan<- listen.TerminalSize) error { return tr.Err }