Skip to content

Commit 3acac5b

Browse files
committed
Migrate PTY from creack/pty to go-pty for Windows support
- Replace creack/pty with aymanbagabas/go-pty v0.2.2 (ConPTY on Windows) - Refactor startPTY to use pty.New() + Command() + Start() pattern - Update ptmx type from *os.File to pty.Pty interface - Simplify resizePTY to use ptmx.Resize() instead of pty.Setsize() - Bump Go version to 1.22 in CI and release workflows - Add error trap in install.ps1 to keep window open on failure
1 parent 34fea2e commit 3acac5b

8 files changed

Lines changed: 31 additions & 23 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Setup Go
1818
uses: actions/setup-go@v5
1919
with:
20-
go-version: '1.21'
20+
go-version: '1.22'
2121

2222
- name: Download dependencies
2323
run: go mod download

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: Setup Go
3939
uses: actions/setup-go@v5
4040
with:
41-
go-version: '1.21'
41+
go-version: '1.22'
4242

4343
- name: Get version from tag
4444
id: version

go.mod

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
module github.com/softwarity/aipilot-cli
22

3-
go 1.19
3+
go 1.20
44

55
require (
6-
github.com/creack/pty v1.1.24
6+
github.com/aymanbagabas/go-pty v0.2.2
77
github.com/google/uuid v1.6.0
88
github.com/gorilla/websocket v1.5.3
99
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
1010
golang.org/x/crypto v0.17.0
1111
golang.org/x/term v0.27.0
1212
)
1313

14-
require golang.org/x/sys v0.28.0 // indirect
14+
require (
15+
github.com/creack/pty v1.1.21 // indirect
16+
github.com/u-root/u-root v0.11.0 // indirect
17+
golang.org/x/sys v0.28.0 // indirect
18+
)

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
2-
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
1+
github.com/aymanbagabas/go-pty v0.2.2 h1:YZREB4eSj+1xdbbItIokX0ekjjeifgJOA+ZvxU4/WM8=
2+
github.com/aymanbagabas/go-pty v0.2.2/go.mod h1:gfvlwH+0U66BCwxJREjJaAOEs9H1OFf3YFjI9WSiZ04=
3+
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
4+
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
35
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
46
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
57
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
68
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
79
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
810
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
11+
github.com/u-root/gobusybox/src v0.0.0-20221229083637-46b2883a7f90 h1:zTk5683I9K62wtZ6eUa6vu6IWwVHXPnoKK5n2unAwv0=
12+
github.com/u-root/u-root v0.11.0 h1:6gCZLOeRyevw7gbTwMj3fKxnr9+yHFlgF3N7udUVNO8=
13+
github.com/u-root/u-root v0.11.0/go.mod h1:DBkDtiZyONk9hzVEdB/PWI9B4TxDkElWlVTHseglrZY=
914
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
1015
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
1116
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=

install.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#>
1111

1212
$ErrorActionPreference = "Stop"
13+
trap { Write-Host "`n$_" -ForegroundColor Red; Read-Host "Press Enter to exit"; exit 1 }
1314

1415
$Repo = "softwarity/aipilot-cli"
1516
$BinaryName = "aipilot-cli.exe"

main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"io"
77
"log"
88
"os"
9-
"os/exec"
109
"os/signal"
1110
"path/filepath"
1211
"runtime"
1312
"strings"
1413
"syscall"
1514
"time"
1615

17-
"github.com/creack/pty"
16+
pty "github.com/aymanbagabas/go-pty"
1817
"golang.org/x/term"
1918
)
2019

@@ -280,14 +279,18 @@ func displayHeader(daemon *Daemon, session, command, workDir, agentVersion strin
280279
}
281280

282281
// startPTY starts the PTY and returns the pty master and command
283-
func startPTY(command, workDir string) (*os.File, *exec.Cmd) {
282+
func startPTY(command, workDir string) (pty.Pty, *pty.Cmd) {
284283
fmt.Printf("Starting %s...\n", command)
285-
cmd := exec.Command(command)
284+
ptmx, err := pty.New()
285+
if err != nil {
286+
log.Fatal("Failed to create PTY:", err)
287+
}
288+
cmd := ptmx.Command(command)
286289
cmd.Dir = workDir
287290
cmd.Env = append(os.Environ(), "TERM=xterm-256color")
288291

289-
ptmx, err := pty.Start(cmd)
290-
if err != nil {
292+
if err := cmd.Start(); err != nil {
293+
ptmx.Close()
291294
log.Fatal("Failed to start PTY:", err)
292295
}
293296
return ptmx, cmd
@@ -450,7 +453,7 @@ func startResizeHandler(daemon *Daemon, resizeChan <-chan os.Signal) {
450453
}
451454

452455
// waitForTermination waits for either a signal or process exit, then cleans up
453-
func waitForTermination(sigChan <-chan os.Signal, cmd *exec.Cmd, daemon *Daemon) {
456+
func waitForTermination(sigChan <-chan os.Signal, cmd *pty.Cmd, daemon *Daemon) {
454457
var exitMsg string
455458

456459
select {
@@ -581,7 +584,7 @@ func main() {
581584
waitForTermination(sigChan, cmd, daemon)
582585
}
583586

584-
func waitForProcess(cmd *exec.Cmd) <-chan error {
587+
func waitForProcess(cmd *pty.Cmd) <-chan error {
585588
ch := make(chan error, 1)
586589
go func() {
587590
ch <- cmd.Wait()

terminal.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import (
44
"time"
5-
6-
"github.com/creack/pty"
75
)
86

97
// schedulePCSwitch schedules a debounced switch to PC mode
@@ -120,10 +118,7 @@ func (d *Daemon) resizePTY(rows, cols uint16) error {
120118
}
121119

122120
d.ptyMu.Lock()
123-
err := pty.Setsize(ptmx, &pty.Winsize{
124-
Cols: cols,
125-
Rows: rows,
126-
})
121+
err := ptmx.Resize(int(cols), int(rows))
127122
d.ptyMu.Unlock()
128123
return err
129124
}

types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"context"
55
"crypto/cipher"
6-
"os"
76
"sync"
87
"time"
98

9+
pty "github.com/aymanbagabas/go-pty"
1010
"github.com/gorilla/websocket"
1111
"golang.org/x/term"
1212
)
@@ -58,7 +58,7 @@ type Daemon struct {
5858
relayConnected bool
5959

6060
// PTY
61-
ptmx *os.File
61+
ptmx pty.Pty
6262

6363
// Session info
6464
session string

0 commit comments

Comments
 (0)