Skip to content

Commit acac4f4

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

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

_examples/ssh-ptystart/main.go

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

1011
"github.com/charmbracelet/ssh"
1112
)
@@ -22,22 +23,31 @@ func main() {
2223
return
2324
}
2425

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

33-
// ProcessState gets populated by pty.Start
34-
for {
35-
if cmd.ProcessState != nil {
36-
break
38+
if runtime.GOOS == "windows" {
39+
// ProcessState gets populated by pty.Start waiting on the process
40+
// to exit.
41+
for cmd.ProcessState != nil {
3742
}
38-
}
3943

40-
s.Exit(cmd.ProcessState.ExitCode())
44+
s.Exit(cmd.ProcessState.ExitCode())
45+
} else {
46+
if err := cmd.Wait(); err != nil {
47+
fmt.Fprintln(s, err)
48+
s.Exit(cmd.ProcessState.ExitCode())
49+
}
50+
}
4151
})
4252

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

0 commit comments

Comments
 (0)