Skip to content

Commit

Permalink
fix: ptystart example for unix
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 9, 2024
1 parent c82fc09 commit f8252fe
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions _examples/ssh-ptystart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"os"
"os/exec"
"runtime"
"time"

"github.com/charmbracelet/ssh"
)
Expand All @@ -22,22 +24,32 @@ func main() {
return
}

cmd := exec.Command("powershell.exe")
cmd.Env = append(os.Environ(), "SSH_TTY=windows-pty", fmt.Sprintf("TERM=%s", pty.Term))
name := "bash"
if runtime.GOOS == "windows" {
name = "powershell.exe"
}
cmd := exec.Command(name)
cmd.Env = append(os.Environ(), "SSH_TTY="+pty.Name(), fmt.Sprintf("TERM=%s", pty.Term))
if err := pty.Start(cmd); err != nil {
fmt.Fprintln(s, err.Error())
s.Exit(1)
return
}

// ProcessState gets populated by pty.Start
for {
if cmd.ProcessState != nil {
break
if runtime.GOOS == "windows" {
// ProcessState gets populated by pty.Start waiting on the process
// to exit.
for cmd.ProcessState == nil {
time.Sleep(100 * time.Millisecond)
}
}

s.Exit(cmd.ProcessState.ExitCode())
s.Exit(cmd.ProcessState.ExitCode())
} else {
if err := cmd.Wait(); err != nil {
fmt.Fprintln(s, err)
s.Exit(cmd.ProcessState.ExitCode())
}
}
})

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

0 comments on commit f8252fe

Please sign in to comment.