Skip to content

Commit e8473b2

Browse files
committed
fix: address multi-platform build
1 parent a35635c commit e8473b2

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

pkg/cmd/exec.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os/signal"
1313
"strings"
1414
"sync"
15-
"syscall"
1615

1716
"github.com/gorilla/websocket"
1817
"github.com/kernel/hypeman-go"
@@ -222,35 +221,21 @@ func runExecInteractive(ws *websocket.Conn) (int, error) {
222221
}
223222
defer term.Restore(int(os.Stdin.Fd()), oldState)
224223

225-
// Handle signals gracefully
224+
// Handle signals gracefully (os.Interrupt is cross-platform)
226225
sigCh := make(chan os.Signal, 1)
227-
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
226+
signal.Notify(sigCh, os.Interrupt)
228227
defer signal.Stop(sigCh)
229228

230-
// Handle SIGWINCH for terminal resize
231-
sigwinch := make(chan os.Signal, 1)
232-
signal.Notify(sigwinch, syscall.SIGWINCH)
233-
defer signal.Stop(sigwinch)
234-
235229
// Mutex to protect WebSocket writes from concurrent access
236230
var wsMu sync.Mutex
237231

232+
// Handle terminal resize events (Unix only, no-op on Windows)
233+
cleanupResize := setupResizeHandler(ws, &wsMu)
234+
defer cleanupResize()
235+
238236
errCh := make(chan error, 2)
239237
exitCodeCh := make(chan int, 1)
240238

241-
// Handle terminal resize events
242-
go func() {
243-
for range sigwinch {
244-
cols, rows, _ := term.GetSize(int(os.Stdout.Fd()))
245-
if rows > 0 && cols > 0 {
246-
msg := fmt.Sprintf(`{"resize":{"rows":%d,"cols":%d}}`, rows, cols)
247-
wsMu.Lock()
248-
ws.WriteMessage(websocket.TextMessage, []byte(msg))
249-
wsMu.Unlock()
250-
}
251-
}
252-
}()
253-
254239
// Forward stdin to WebSocket
255240
go func() {
256241
buf := make([]byte, 32*1024)
@@ -380,4 +365,3 @@ func runExecNonInteractive(ws *websocket.Conn) (int, error) {
380365
return 0, nil
381366
}
382367
}
383-

pkg/cmd/exec_unix.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//go:build unix
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"os/signal"
9+
"sync"
10+
"syscall"
11+
12+
"github.com/gorilla/websocket"
13+
"golang.org/x/term"
14+
)
15+
16+
// setupResizeHandler listens for SIGWINCH signals and sends resize messages over the WebSocket.
17+
// Returns a cleanup function that should be deferred.
18+
func setupResizeHandler(ws *websocket.Conn, wsMu *sync.Mutex) (cleanup func()) {
19+
sigwinch := make(chan os.Signal, 1)
20+
signal.Notify(sigwinch, syscall.SIGWINCH)
21+
22+
go func() {
23+
for range sigwinch {
24+
cols, rows, _ := term.GetSize(int(os.Stdout.Fd()))
25+
if rows > 0 && cols > 0 {
26+
msg := fmt.Sprintf(`{"resize":{"rows":%d,"cols":%d}}`, rows, cols)
27+
wsMu.Lock()
28+
ws.WriteMessage(websocket.TextMessage, []byte(msg))
29+
wsMu.Unlock()
30+
}
31+
}
32+
}()
33+
34+
return func() { signal.Stop(sigwinch) }
35+
}

pkg/cmd/exec_windows.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build windows
2+
3+
package cmd
4+
5+
import (
6+
"sync"
7+
8+
"github.com/gorilla/websocket"
9+
)
10+
11+
// setupResizeHandler is a no-op on Windows since SIGWINCH doesn't exist.
12+
// Terminal resize events are not supported on native Windows.
13+
// The initial terminal size is still sent in the exec request.
14+
func setupResizeHandler(ws *websocket.Conn, wsMu *sync.Mutex) (cleanup func()) {
15+
return func() {}
16+
}

0 commit comments

Comments
 (0)