| 1 | //go:build windows |
| 2 | |
| 3 | package persistentshell |
| 4 | |
| 5 | import ( |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/UserExistsError/conpty" |
| 9 | "golang.org/x/sys/windows" |
| 10 | ) |
| 11 | |
| 12 | type windowsPTY struct { |
| 13 | pty *conpty.ConPty |
| 14 | closeOnce sync.Once |
| 15 | } |
| 16 | |
| 17 | func startPTY(argv []string, dir string, env []string) (ptyConn, error) { |
| 18 | if len(argv) == 0 { |
| 19 | return nil, errEmptyArgv |
| 20 | } |
| 21 | if !conpty.IsConPtyAvailable() { |
| 22 | return nil, conpty.ErrConPtyUnsupported |
| 23 | } |
| 24 | commandLine := windows.ComposeCommandLine(argv) |
| 25 | p, err := conpty.Start( |
| 26 | commandLine, |
| 27 | conpty.ConPtyDimensions(80, 24), |
| 28 | conpty.ConPtyWorkDir(dir), |
| 29 | conpty.ConPtyEnv(env), |
| 30 | ) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | return &windowsPTY{pty: p}, nil |
| 35 | } |
| 36 | |
| 37 | func (c *windowsPTY) Read(p []byte) (int, error) { |
| 38 | return c.pty.Read(p) |
| 39 | } |
| 40 | |
| 41 | func (c *windowsPTY) Write(p []byte) (int, error) { |
| 42 | return c.pty.Write(p) |
| 43 | } |
| 44 | |
| 45 | func (c *windowsPTY) Close() error { |
| 46 | var closeErr error |
| 47 | c.closeOnce.Do(func() { |
| 48 | if c.pty != nil { |
| 49 | closeErr = c.pty.Close() |
| 50 | } |
| 51 | }) |
| 52 | return closeErr |
| 53 | } |
| 54 |