Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed Nov 13, 2024
1 parent 33541bd commit 1828e96
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion commands/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion commands/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions do/mocks/Listen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/listen/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/listen/listen_resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 0 additions & 19 deletions pkg/listen/listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
5 changes: 3 additions & 2 deletions pkg/listen/listen_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1828e96

Please sign in to comment.