Skip to content

Commit f8252fe

Browse files
committed
fix: ptystart example for unix
1 parent c82fc09 commit f8252fe

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

_examples/ssh-ptystart/main.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log"
77
"os"
88
"os/exec"
9+
"runtime"
10+
"time"
911

1012
"github.com/charmbracelet/ssh"
1113
)
@@ -22,22 +24,32 @@ func main() {
2224
return
2325
}
2426

25-
cmd := exec.Command("powershell.exe")
26-
cmd.Env = append(os.Environ(), "SSH_TTY=windows-pty", fmt.Sprintf("TERM=%s", pty.Term))
27+
name := "bash"
28+
if runtime.GOOS == "windows" {
29+
name = "powershell.exe"
30+
}
31+
cmd := exec.Command(name)
32+
cmd.Env = append(os.Environ(), "SSH_TTY="+pty.Name(), fmt.Sprintf("TERM=%s", pty.Term))
2733
if err := pty.Start(cmd); err != nil {
2834
fmt.Fprintln(s, err.Error())
2935
s.Exit(1)
3036
return
3137
}
3238

33-
// ProcessState gets populated by pty.Start
34-
for {
35-
if cmd.ProcessState != nil {
36-
break
39+
if runtime.GOOS == "windows" {
40+
// ProcessState gets populated by pty.Start waiting on the process
41+
// to exit.
42+
for cmd.ProcessState == nil {
43+
time.Sleep(100 * time.Millisecond)
3744
}
38-
}
3945

40-
s.Exit(cmd.ProcessState.ExitCode())
46+
s.Exit(cmd.ProcessState.ExitCode())
47+
} else {
48+
if err := cmd.Wait(); err != nil {
49+
fmt.Fprintln(s, err)
50+
s.Exit(cmd.ProcessState.ExitCode())
51+
}
52+
}
4153
})
4254

4355
log.Println("starting ssh server on port 2222...")

0 commit comments

Comments
 (0)